Codingbat maxMirror challenge I request you to provide insightful suggestions to improve the following code as well as an alternative algorithm to solve it.
Problem Specification:

We'll say that a &quot;mirror&quot; section in an array is a group of contiguous elements such that somewhere in the array, the same group appears in reverse order. For example, the largest mirror section in {1, 2, 3, 8, 9, 3, 2, 1} is length 3 (the {1, 2, 3} part). Return the size of the largest mirror section found in the given array.
maxMirror({1, 2, 3, 8, 9, 3, 2, 1}) → 3
maxMirror({1, 2, 1, 4}) → 3
maxMirror({7, 1, 2, 9, 7, 2, 1}) → 2

Solution:
public class MaxMirror {
    public static void main(String[] args) {
        MaxMirror max = new MaxMirror();
        int[] nums = { 1, 2, 1, 4 };
        System.out.println(&quot;The maximum length of the mirror is &quot;
                + max.maxMirror(nums));
    }

    public int maxMirror(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        // Reverse order of the array
        int[] revNums = new int[nums.length];
        for (int numIndex = nums.length - 1, revIndex = 0; numIndex &gt;= 0; numIndex--, revIndex++) {
            revNums[revIndex] = nums[numIndex];
        }
        // Hope the sub array of maximum length is the array itself
        int subArraysToBeChecked = 1;
        for (int len = nums.length; len &gt; 1; len--) {
            if (mirrorOfLengthLenExists(nums, revNums, len,
                    subArraysToBeChecked)) {
                return len;
            }
            // Increase the number of sub-arrays to be checked
            subArraysToBeChecked++;
        }
        // In the worst case return 1
        return 1;
    }

    // Choose start and end i.e. sub-arrays to be checked
    public boolean mirrorOfLengthLenExists(int[] nums, int[] revNums, int len,
            int subArraysToBeChecked) {
        int start = 0;
        int end = len - 1;
        for (int i = len; i &lt;= nums.length; i++) {
            if (mirrorExistsFromStartToEnd(nums, revNums, start, end,
                    subArraysToBeChecked)) {
                return true;
            }
            start++;
            end++;
        }
        return false;
    }

    // Check from start to end whether mirrored in the revNums
    public boolean mirrorExistsFromStartToEnd(int[] nums, int[] revNums,
            int start, int end, int subArraysToBeChecked) {
        int revStart = 0;
        for (int subArraysChecked = 1; subArraysChecked &lt;= subArraysToBeChecked; subArraysChecked++) {
            int noOfElementsEqual = 0;
            for (int numIndex = start, revIndex = revStart; numIndex &lt;= end; numIndex++, revIndex++) {
                if (revNums[revIndex] != nums[numIndex]) {
                    break;
                } else {
                    ++noOfElementsEqual;
                }
            }
            if (noOfElementsEqual == (end - start + 1)) {
                return true;
            }
            revStart++;
        }
        return false;
    }
}
My approach is to have two indexes. The i index starts at the left and the j index starts at the right of the array. For a length of 4, we would do these comparisons in this order.
(0, 3), (0, 2), (0, 1), then (1, 3), (1, 2), then, (2, 3) This is basically a pairwise comparison. (Note the initialization part of the for loop is empty)

int frontIndex = 0;
int endIndex = sequence.length - 1;
for( ; frontIndex &lt; endIndex; frontIndex++)
{
    for(int i = endIndex ; i &gt; frontIndex; i--)
    {
    //stuff here
    }
}


Once a match is found, we start counting the max length, with one index moving to the left and one to the right. The while condition makes sure we don't go out of bounds. Once we break out of that loop, we may update the max length.

while(frontIndex + currentLength &lt; sequence.length &amp;&amp; i - currentLength &gt;= 0)
{
    if(sequence[frontIndex + currentLength] == sequence[i - currentLength]) 
    {
        currentLength++;
    }
    else break;
}
if (currentLength &gt; maxLength) maxLength = currentLength;
currentLength = 0;


This is the entire class

public class Mirrors 
{
    public static void main(String[] args)
    {
        int maxLength = 1;
        int currentLength = 0;
        int[] sequence = {1, 2, 1, 2, 1};
        int frontIndex = 0;
        int endIndex = sequence.length - 1;
        for( ; frontIndex &lt; endIndex; frontIndex++)
        {
            for(int i = endIndex; i &gt; frontIndex; i--)
            {
                if(sequence[frontIndex] == sequence[i])
                {
                    currentLength++;
                    while(frontIndex + currentLength &lt; sequence.length &amp;&amp; i - currentLength &gt;= 0)
                    {
                        if(sequence[frontIndex + currentLength] == sequence[i - currentLength]) 
                        {
                            currentLength++;
                        }
                        else break;
                    }
                    if (currentLength &gt; maxLength) maxLength = currentLength;
                    currentLength = 0;
                }
            }
        }
        System.out.print("Max length is " + maxLength);
    }
}