Deep_Dev
article thumbnail

 

 


import java.util.*;

class Solution {
    public int[] solution(int[] numlist, int n) {
        int[] answer=new int[numlist.length];
        ArrayList<Integer> list=new ArrayList<>();
        for(int i=0; i<numlist.length; i++){
            list.add(numlist[i]);
        }
        list.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer a, Integer b) {
                int absA=Math.abs(a-n);
                int absB=Math.abs(b-n);

                if(absA==absB){
                    if(a>b){
                        return -1;
                    }else{
                        return 1;
                    }
                }else{
                    return absA-absB;
                }
            }
        });
        for(int i=0; i<list.size(); i++){
            answer[i]=list.get(i);
        }
        return answer;
    }
}