Skip to content

Minimum Distances

MoustafaAttia edited this page Nov 29, 2017 · 1 revision
This wiki to show my solution for HackerRank problem [[https://www.hackerrank.com/challenges/minimum-distances/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: #+BEGIN_EXAMPLE 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) #+END_EXAMPLE

Clone this wiki locally