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!#Sortthearraymin_diff=Float::INFINITYresult=[]# Iterate through the array to find the minimum difference(0...arr.length-1).eachdo|i|diff=(arr[i+1]-arr[i]).absifdiff<min_diffmin_diff=diffresult=[[arr[i],arr[i+1]]]#Resettheresultwiththenewclosestpairelsifdiff==min_diffresult<<[arr[i],arr[i+1]]#Addthepairifithasthesamemindifferenceendendresultend
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 Simple Ruby Solution