Basically I have university work about recursion but I'm having problems solving this. I have to create two methods, one called getLastElement and isSymmetric. getLastElement can only access index 0 from the array. isSymmetric must print true if the array is symmetric or if it's 0. It must use array[0] and array.length. It can also use Arrays.copyOfRange()
I already made isSymmetric but without getLastElement and I think I'm missing something since I don't know how to incorporate getLastElement into it. I know I didn't use array[0] but I couldn't get the code working with it.
Here's my code:
public static int isSymmetric(int array[], int begin, int end)
{
if (begin >= end) {
return 1;
}
if (array[begin] == array[end]) {
return isSymmetric(array, begin + 1, end - 1);
}
else {
return 0;
}
}
public static void main (String[] args) {
int array[] = { 1, 2, 3, 2, 1 };
if (isSymmetric(array, 0, array.length - 1) == 1)
System.out.print( "true");
else
System.out.println( "false");
}
I just want to print just like I am right now but with getLastElement incorporated into isSymmetric.