You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MoustafaAttia edited this page Nov 29, 2017
·
1 revision
This wiki to show my solution for HackerRank problem Minimum Distances
Solution steps:
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
Solution in Python 3:
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)