Determine if two arrays are a rotated version of each other Following algorithm has time complexity \$O(N)\$ and space complexity \$O(1)\$.

Any suggestions to improve this algorithm or any alternate approach suggestions?    

public static boolean isRotatedVersion(int[] a, int[] b){
        //Check length of arrays
        if(a.length != b.length)
            return false;

        //Declare two pointers. One for each array
        int aPointer = 0;
        int bPointer = 0;

        //This flag will prevent us walking through the 2nd array in a loop only once
        //Helps avoid going into infinite walk loop
        boolean isFirstTimeWalk = true;

        //This loop will run from 0 to n of array 1
        while(aPointer &lt; a.length){

            //If elements at position aPointer and bPointer are equal then incr both pointer 
            // to move to the element in both array
            if(a[aPointer] == b[bPointer]){
                aPointer++;
                if(bPointer &lt; b.length-1)
                    bPointer++;
                else{ //If reached end of second array then it reset pointer to 0 to rotate
                    //Once rotated done mark is1stTimeWalked to false so avoid future loops
                    isFirstTimeWalk = false;
                    bPointer = 0;
                }
            }else if(isFirstTimeWalk &amp;&amp; bPointer &lt; b.length-1){
                //Move bPointer until a match is found
                bPointer++;
            }else {
                return false;
            }
        }
        return true;
    }
}
Bug

I ran your code with the following arrays and your function thought that the two arrays were rotated versions of each other but they were not:

public static void main(String [] args)
{
    int [] a = { 0, 1, 2, 3, 0 };
    int [] b = { 0, 1, 1, 2, 3 };
    System.out.println(isRotatedVersion(a, b));
}


Output:


  true


Achieving a correct \$O(n)\$ solution

Determining if an array is a rotated version of another array is equivalent to searching for the first array within the second array concatenated with itself.  For example, given:

a = { 0, 0, 1, 2, 0 }
b = { 0, 0, 0, 1, 2 }


You can search for pattern a within this array, which is b doubled:

{ 0, 0, 0, 1, 2, 0, 0, 0, 1, 2 }


To do this in \$O(n)\$ time, you just need to implement a linear time string searching algorithm.  I would suggest using the Knuth Morris Pratt algorithm, which is easy to understand and implement.