"""
Given 2 integer arrays, determine of the 2nd array is a rotated version of the 1st array.

Ex. Original Array A={1,2,3,5,6,7,8} Rotated Array B={5,6,7,8,1,2,3}
"""


def is_rotation(arr1, arr2):
    if len(arr1) != len(arr2):
        return False

    j = 0
    while arr2[j] != arr1[0] and j < len(arr2):
        j += 1

    if j >= len(arr2):
        return False

    i = 0
    while i < len(arr1):
        if arr1[i] != arr2[j]:
            return False
        i += 1
        j = (j + 1) % len(arr2)

    return True


def is_rotation_duplicates(arr1, arr2):
    if len(arr1) != len(arr2):
        return False

    i = 0
    j = 0
    n = len(arr1)
    while i < n and j < n:
        k = 1
        while k <= n and arr1[(i + k) % n] == arr2[(j + k) % n]:
            k += 1
        if k > n:
            return True
        i += k
    return False


if __name__ == '__main__':
    print is_rotation_duplicates([1, 2, 3, 5, 6, 7, 8], [5, 6, 7, 8, 1, 2, 3])         # True
    print is_rotation_duplicates([1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8])   # True
    print is_rotation_duplicates([1, 2, 3, 4, 5, 6, 7, 8], [5, 6, 7, 8, 1, 2, 3])      # False
    print is_rotation_duplicates([2, 4, 3, 1, 7, 8, 6, 5], [6, 5, 2, 4, 3, 1, 7, 8])   # True
    print is_rotation_duplicates([2, 4, 2, 1, 7, 8, 6, 5], [6, 5, 2, 4, 2, 1, 7, 8])   # True

