Write the definition of a method, isReverse, whose two parameters are arrays of integers of equal size. The method returns true if and only if one array is the reverse of the other. (“Reverse” here means same elements but in reverse order.)

1
Write the definition of a method, isReverse, whose two parameters are arrays of integers of equal size. The method returns true if and only if one array is the reverse of the other. (“Reverse” here means same elements but in reverse order.)

LANGUAGE: JAVA

CHALLENGE:

Write the definition of a method, isReverse, whose two parameters are arrays of integers of equal size. The method returns true if and only if one array is the reverse of the other. (“Reverse” here means same elements but in reverse order.)

SOLUTION:

boolean isReverse(int a[], int b[]) {
   int k;
   for (k = 0; k < a.length && a[k] == b[a.length-1-k]; k++);
   return k == a.length;
}

One comment

  • fly says:

    public static boolean isReverse(int a[], int b[]) {
    boolean result = false;

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

    if (a[i] == b[b.length – i – 1]) {
    result = true;
    } else {
    result = false;
    break;
    }
    }
    return result;
    }

Add comment