Java Program A array palindrome is an array which when its e

Java Program:

A \'array palindrome\' is an array which, when its elements are reversed, remains the same (i.e., the elements of the array are same when scanned forward or backward) Write a recursive, boolean -valued  method , isPalindrome, that accepts an integer -valued  array , and a pair ofintegers representing the starting and ending indexes of the portion of the array to be tested for being a palindrome. The function returns whether that portion of the array is a palindrome.

An array is a palindrome if:

the array is empty (0 elements ) or contains only one element (which therefore is the same when reversed), or

the first and last elements of the array are the same, and the rest of the array (i.e., the second through next-to-last elements ) form a palindrome.

Solution

Hi, friend, Please find my implementation.

Please let me know in case of any issue.

public class Test {

  

  

   public static boolean isPalindrome(int[] arr, int start, int end){

      

       // base case

       if(arr == null || (end-start)==1 || (end-start)==0)

           return true;

      

       // compare start and end element and start+1, end-1 and so on

       while(start < end){

           if(arr[start] != arr[end])

               return false;

           start++;

           end--;

       }

      

       return true;

   }

   public static void main(String[] args) {

      

       int arr[] = {4,5,6,7,6,5,4};

      

       System.out.println(\"IsPallindrome: \"+isPalindrome(arr, 0, 6));

      

   }

}

Java Program: A \'array palindrome\' is an array which, when its elements are reversed, remains the same (i.e., the elements of the array are same when scanned
Java Program: A \'array palindrome\' is an array which, when its elements are reversed, remains the same (i.e., the elements of the array are same when scanned

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site