✔3 approaches explained || Heap || Binary Search || Two pointers || Sliding window
Method - 1 \nusing heap \n\n# Code\n\nclass Solution {\npublic:\n    vector<int> findClosestElements(vector<int>& arr, int k, int x) {\n        \n        // min
## Method - 1 \nusing heap \n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> findClosestElements(vector<int>& arr, int k, int x) {\n        \n        // min heap\n        // push all pairs - { abs(x - arr[i]) , arr[i]) }\n        priority_queue<pair<int,int>,\n        vector<pair<int,int>>,\n        greater<pair<int,int>>\n        >pq;\n        vector<int>res;\n\n        // push into heap , absolute difference and element pair \n        for(int i=0;i<arr.size();i++)\n        {\n            pq.push({abs(x - arr[i]) , arr[i]});\n        }\n        // pop first k elements \n        while(k--)\n        {\n            auto top = pq.top();\n            pq.pop();\n            res.push_back(top.second);\n        }\n        // sort in ascending order \n        sort(res.begin(),res.end());\n        return res;\n\n    }\n};\n```\n\n\n\n## Method - 2 \nBinary search and two pointers\n\n# Code \n```\nclass Solution {\npublic:\n\n    // bound()\n    // returns index say \'ind\', where nums[ind] <= x\n    int bound(vector<int>&nums,int x)\n    {\n        int low = 0 , high = nums.size() - 1;\n        int mid;\n        while(low <= high)\n        {\n            mid = low + (high-low)/2;\n            if(nums[mid] == x or nums[mid] < x) low = mid + 1;\n            else\n                high = mid - 1;   \n        }\n        return low;\n    }\n\n    vector<int> findClosestElements(vector<int>& arr, int k, int x) {\n        int ind =  bound(arr,x); // find index where nums[ind] is equal or just less than \'x\' \n        int i = ind - 1 , j = ind; // two pointers\n        int n = arr.size();\n        while(k--)\n        {\n            if(i >= 0 and j < n)\n            {\n                // expand i to left \n                if(abs(arr[i] - x) <= abs(arr[j] - x )) \n                    i--;\n                else \n                // expand j to right\n                    j++;\n            }\n            else\n            if(i < 0)\n            {\n                j++;\n            }\n            else\n            if(j >= n)\n            {\n                i--;\n            }\n        }\n    return vector<int>(arr.begin()+i+1,arr.begin()+j);\n    }\n};\n```\n\n## Method - 3 \nTwo Pointers + Sliding window\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> findClosestElements(vector<int>& arr, int k, int x) {\n        int  l = 0 , h = arr.size()-1;\n        // two pointers \n        \n        // while window size > k \n        while( (h-l+1) > k )\n        {\n            // shrink from left\n            if(abs(arr[l]- x) > abs(arr[h] -x)){\n                l++;\n            }\n            // shrink from right\n            else{\n                h--;\n            }\n        }\n        return vector<int> (arr.begin()+l, arr.begin()+h+1);\n    }\n};\n```
['Array', 'Two Pointers', 'Binary Search', 'Sliding Window', 'Heap (Priority Queue)', 'C++']