Problem :

https://www.hackerrank.com/challenges/minimum-distances/problem


My Solution :

#!/usr/bin/env python3


def minimum_distances(a):
    min_d = float('inf')
    last_index = {}
    for i, num in enumerate(a):
        if num in last_index:
            min_d = min(min_d, i-last_index[num])
        last_index[num] = i
    return min_d if min_d != float('inf') else -1


n = int(input())
a = list(map(int, input().rstrip().split()))
result = minimum_distances(a)
print(result)