All 3 Approaches MinHeap, Two Pointers, BinarySearch
Upvote if you Like the solutions.\n2. 1. Priority Queue(optimizations can be done here)\n\nclass Solution {\npublic:\n    class Pair{\n      public:\n        in
Upvote if you Like the solutions.\n2. 1. Priority Queue(optimizations can be done here)\n```\nclass Solution {\npublic:\n    class Pair{\n      public:\n        int dist;\n        int ele;\n        Pair(int d,int e) {\n            dist=d;\n            ele=e;\n        }\n    };\n    class Compare {\n        public:\n        bool operator()(Pair a, Pair b) {\n            return a.dist < b.dist;\n        }\n    };\n    vector<int> findClosestElements(vector<int>& arr, int k, int x) {\n        int n = arr.size();\n        priority_queue<Pair,vector<Pair>,Compare> pq;\n        \n        for(int i=0;i<n;i++) {\n            if(pq.size() < k) {\n                int dist = abs(arr[i]-x);\n                pq.push(Pair(dist,arr[i]));\n            } else {\n                Pair top = pq.top();\n                int dist = top.dist;\n                int ele = top.ele;\n                if(dist > abs(arr[i]-x)) {\n                    pq.pop();\n                    pq.push({abs(arr[i]-x), arr[i]});\n                }\n            }\n        }\n        \n        vector<int> ans;\n        while(!pq.empty()) {\n            ans.push_back(pq.top().ele);\n            pq.pop();\n        }\n        \n        sort(ans.begin(),ans.end());\n        return ans;\n    }\n};\n```\n\n2.Two Pointers Approach\n```\nvector<int> findClosestElements(vector<int>& arr, int k, int x) {\n        int i=0;\n        int j=arr.size()-1;\n        \n        while(i < j && j-i+1 != k) {\n            int diff1 = abs(arr[i] - x);\n            int diff2 = abs(arr[j] - x);\n            if(diff1 <= diff2) {\n                j--;\n            } else {\n                i++;\n            }\n        }\n        \n        vector<int> ans;\n        for(int x=i;x<=j;x++) {\n            ans.push_back(arr[x]);\n        }\n        return ans;\n    }\n```\n\n3.Binary Search\n```\nvector<int> findClosestElements(vector<int>& arr, int k, int x) {\n        int n = arr.size();\n        int low = 0;\n        int high = n-k;\n        \n        while(low < high) {\n            int mid = low + (high - low) / 2;\n            \n            int midStart = x - arr[mid];\n            int midEnd = arr[mid + k] - x;\n            \n            if(midStart > midEnd) {\n                low = mid+1;\n            } else {\n                high = mid;\n            }\n        }\n        vector<int> ans;\n        for(int i=low;i<low+k;i++) {\n            ans.push_back(arr[i]);\n        }\n        return ans;\n    }\n```
['Two Pointers', 'Binary Search', 'C', 'Heap (Priority Queue)', 'Binary Tree']