You are viewing a single comment's thread. Return to all comments →
Steps to solve simple:
below is the implementation:
public static List<Integer> closestNumbers(List<Integer> arr) { arr.sort((a,b) -> a.compareTo(b)); int minDiff = Integer.MAX_VALUE; for(int i=1;i<arr.size();i++) minDiff = Math.min(minDiff, arr.get(i) - arr.get(i-1)); List<Integer> res = new ArrayList<>(); for(int i=1;i<arr.size();i++) if(arr.get(i) - arr.get(i-1) == minDiff) { res.add(arr.get(i-1)); res.add(arr.get(i)); } return res; }
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 →
Steps to solve simple:
below is the implementation: