Codingbat maxMirror challenge
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);
    }
}