56.8k views
18 votes
Write a program in Java to implement a recursive boolean function verify that returns True if the given array contents remain the same when array is reversed, i.e., when last element is equal to the first element, second last element is equal to the second element and so on. Otherwise function verify must returns False. Examples: Input: arr[] = {12, 32, 67, 32, 12} Output: True Input: arr[] = {1, 2, 3, 4, 5} Output: False

User Acr
by
4.8k points

1 Answer

4 votes

Answer:

class Main {

public static boolean verify(int[] a) {

for(int i=0;i<a.length/2;i++) {

if (a[i] != a[a.length-i-1]) {

return false;

}

}

return true;

}

public static void main(String[] args) {

int arr1[] = {12, 32, 67, 32, 12};

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

System.out.println("arr1: " + (verify(arr1) ? "True" : "False"));

System.out.println("arr2: " + (verify(arr2) ? "True" : "False"));

}

}

Step-by-step explanation:

This is called a palindrome.

User Blackbrandt
by
5.2k points