Java A array palindrome is an array which when its elements

Java

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 of integers 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

/**
* @author kumar
*
*/
public class Tester {
  
   public static void main(String args[]){
       System.out.println(isPalindrome(new int[]{1,2,2,1}, 0, 3));
   }
   /**
   * @param array
   * @param startIndex
   * @param endIndex
   * @return A boolean representing if array is Palindrome or not
   */
   public static boolean isPalindrome(int array[], int startIndex, int endIndex){
       if(startIndex < endIndex){
           if(array[startIndex] == array[endIndex]){
               return isPalindrome(array, startIndex+1, endIndex-1);
           } else {
               return false;
           }
       }
       return true;
   }
}

Java 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 o

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site