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.
publicstaticList<Integer>closestNumbers(List<Integer>arr){// Write your code hereCollections.sort(arr);List<Integer>res=newArrayList<Integer>();intmin=200000000;for(inti=0;i<arr.size()-1;i++){inth=Math.abs(arr.get(i)-arr.get(i+1));if(h<min){min=h;res.clear();res.add(arr.get(i));res.add(arr.get(i+1));}elseif(h==min){res.add(arr.get(i));res.add(arr.get(i+1));}}returnres;}
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 →
JAVA Solution :