We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defclosestNumbers(arr):result=[]n=len(arr)arr.sort()min_d=10000000# First find the minimum differenceforiinrange(n-1):ifabs(arr[i]-arr[i+1])<=min_d:min_d=abs(arr[i]-arr[i+1])# Then find all the pair# Which minimum difference is equal to the found minimum differenceforiinrange(n-1):ifabs(arr[i]-arr[i+1])<=min_d:result.extend([arr[i],arr[i+1]])returnresult
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Closest Numbers
You are viewing a single comment's thread. Return to all comments →