Java Heap solution.
\nclass Solution {\n    class Pair{\n        int key;\n        int value;\n    }\n    public List<Integer> findClosestElements(int[] arr, int k, int x) {\n     
```\nclass Solution {\n    class Pair{\n        int key;\n        int value;\n    }\n    public List<Integer> findClosestElements(int[] arr, int k, int x) {\n        PriorityQueue<Pair> pq=new PriorityQueue<>(\n            new Comparator<Pair>(){\n                public int compare(Pair a,Pair b){\n                    //if(a.key>b.key)return 1;\n                     return b.key-a.key;\n                }\n            }\n        );\n        int n=arr.length;\n        for(int i=0;i<k;i++){\n             Pair temp=new Pair();\n            temp.key=Math.abs(arr[i]-x);\n            temp.value=arr[i];\n            pq.add(temp);\n        }\n        for(int i=k;i<n;i++){\n             Pair temp=new Pair();\n            temp.key=Math.abs(arr[i]-x);\n            temp.value=arr[i];\n            \n            Pair check=pq.peek();\n            if(check.key>temp.key){\n                pq.add(temp);\n                pq.poll();\n            }\n        }\n      \n        List<Integer> res=new ArrayList<>();\n        while(pq.size()>0){\n            Pair p=pq.peek();\n            res.add(p.value);\n            pq.poll();\n        }\n     Collections.sort(res);\n        return res;\n    }\n}\n```
['Heap (Priority Queue)', 'Java']