-
Notifications
You must be signed in to change notification settings - Fork 0
Minimum Distances
MoustafaAttia edited this page Nov 29, 2017
·
1 revision
This wiki to show my solution for HackerRank problem Minimum Distances
- set the result variable as maximum possible value + 1
- Iterate over all elements in array
- in each iteration check if this element exist before or this is the first time this element appear
- if first time save it’s index in dictionary
- if appear before compare new index with previous index, and store in result variable the minimum so far
import sys
n = int(input().strip())
A = [int(A_temp) for A_temp in input().strip().split(' ')]
d = dict()
res = (10**5) + 1
for i in range(0,len(A)) :
if A[i] not in d :
d[A[i]] = i
else :
res = min(i - d[A[i]],res)
if res == (10**5) + 1 :
print (-1)
else :
print (res)