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 arr.sort()result={}forindexinrange(len(arr)):if(index+1<len(arr)):current_dif=abs(arr[index]-arr[index+1])if(current_difinresult):result[current_dif].append((arr[index],arr[index+1]))else:result[current_dif]=[(arr[index],arr[index+1])]min_key=min(list(result.keys()))arr_result=result[min_key]final_result=[]foriteminarr_result:final_result.extend(item)returnfinal_result
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 →
Solution using dictionary