Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Program: A \'array palindrome\' is an array which, when its elements are re

ID: 3924341 • Letter: J

Question

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.

Explanation / Answer

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));

      

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote