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 herearr.sort()min_abs=abs(arr[0]-arr[1])min_arr=[arr[0],arr[1]]foriinrange(1,len(arr)-1):ifabs(arr[i]-arr[i+1])>min_abs:continueelifabs(arr[i]-arr[i+1])==min_abs:min_arr.append(arr[i])min_arr.append(arr[i+1])elifabs(arr[i]-arr[i+1])<min_abs:min_abs=abs(arr[i]-arr[i+1])min_arr=[arr[i],arr[i+1]]returnmin_arr
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 →