Find K Closest Elements || JAVA || HEAP || LAMBDA FUNCTION || SIMPLE CODE
Simple Comparator using lambda function code!!!\nSimply first sorting on the basis of the difference and if both are equal then on the basis of arr[i]...\n\n\nc
Simple Comparator using lambda function code!!!\nSimply first sorting on the basis of the difference and if both are equal then on the basis of arr[i]...\n\n```\nclass Solution {\n    public List<Integer> findClosestElements(int[] arr, int k, int x) {\n        PriorityQueue<Pair<Integer,Integer>> pq=new PriorityQueue<>((a,b)-> b.getKey()!=a.getKey()?b.getKey()-a.getKey():b.getValue()-a.getValue());\n        for(int i=0;i<arr.length;i++){\n            pq.add(new Pair <>(Math.abs(x-arr[i]),arr[i]));\n        }\n        while(pq.size()>k){\n            pq.remove();\n        }\n        LinkedList<Integer> list=new LinkedList<>();\n        while(!pq.isEmpty()){\n            list.addFirst(pq.remove().getValue());\n        }\n        Collections.sort(list);\n        return list;\n    }\n}\n```\nPlease Upvote If U Like My Explanation
['Sorting', 'Heap (Priority Queue)', 'Java']