You are viewing a single comment's thread. Return to all comments →
Collections.sort(arr); List<Integer> list=new ArrayList<>(); int minDiff=Integer.MAX_VALUE; int diff; for(int i=0;i<arr.size()-1;i++){ diff= arr.get(i+1)-arr.get(i); diff=Math.abs(diff); if(diff<minDiff){ list.clear(); minDiff=diff; list.add(arr.get(i)); list.add(arr.get(i+1)); }else if(diff==minDiff){ list.add(arr.get(i)); list.add(arr.get(i+1)); } } return list;
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