Created
February 22, 2021 08:25
-
-
Save samarthsewlani/0fb0a15f6464253fdaceec8c1a075bbc to your computer and use it in GitHub Desktop.
Minimum Distances HackerRank Solution ( Brute Force )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def minimumDistances(a): | |
n=len(a) | |
minimum=n+1 #Initializing with n+1 because if no updates take place we should return -1 as there are no matching pairs | |
# Note:- If any matching pairs is found the distance would be less than n | |
for i in range(n-1): | |
for j in range(i+1,n): | |
if a[i]==a[j]: | |
if j-i<minimum: | |
minimum=j-i | |
if minimum==n+1: | |
return -1 | |
return minimum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment