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):# Write your code hereresult=[]minDiff=sys.maxsize# Sort the list in ascending orderarr.sort()# Loop to find the minimum difference of two pairsforiinrange(len(arr)-1):if(arr[i+1]-arr[i])<minDiff:minDiff=arr[i+1]-arr[i]# Loop to add the minimum difference pairs to the result listforiinrange(len(arr)-1):if(arr[i+1]-arr[i])==minDiff:result.append(arr[i])result.append(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 →
Here is my solution using Python3: