# your code goes here res = max(res, i - mp[arr[i]])
# Python Program to find max distance between two occurrences
# in array using hashing

def maxDistance(arr):
  
    # Stores element to first index mapping
    mp = {}
    res = 0

    for i in range(len(arr)):
      
        # If this is the first occurrence of the
        # element, store its index
        if arr[i] not in mp:
            mp[arr[i]] = i
            
        # Else update max distance
        else:
            res = max(res, i - mp[arr[i]])

    return res

arr = [1, 1, 2, 2, 2, 1]
print(maxDistance(arr))
 