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

Write a static recursive method named forwardsEqualBackwards that returns the va

ID: 3810599 • Letter: W

Question

Write a static recursive method named forwardsEqualBackwards that returns the value true if the sequence of elements in an array of integers is the same irrespective of whether the array is traversed forwards or backwards and it returns false, otherwise.

Create a project named Project8 containing a class named Program8 which contains the main method for this application as well as the method forwardsEqualBackwards. The main method for this application reads in the size of an input array as well as its elements from the keyboard and then calls the method forwardsEqualBackwards to output the result.

Explanation / Answer

HI, Please find my implementation.

public class Program8 {

   public static boolean forwardsEqualBackwards(int[] arr){

      

       // base check

       if(arr == null || arr.length <= 1)

           return true;

      

       return forwardsEqualBackwardsHelper(arr, 0, arr.length-1);

   }

   public static boolean forwardsEqualBackwardsHelper(int[] arr, int i, int j){

      

       if(i > j)

           return false;

       if(i == j)

           return true;

      

       if(arr[i] != arr[j])

           return false;

      

       else

           return forwardsEqualBackwardsHelper(arr, i+1, j-1);

   }

   public static void main(String[] args) {

      

       int arr[] = {1,2,3,4,3,2};

       int arr1[] = {1,2,3,4,3,2, 1};

       int arr2[] = {1,2,3,4,5,4,3,2,1};

      

       System.out.println(forwardsEqualBackwards(arr));

       System.out.println(forwardsEqualBackwards(arr1));

       System.out.println(forwardsEqualBackwards(arr2));

   }

}

/*

Sample run:

false

true

true

*/

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