C++ (3 detailed and easy to understand approaches)
\n//Approach-1 (Using priority queue : O((n+k)logk) (Time : 84ms)\nclass Solution {\npublic:\n    typedef pair<int, int> P;\n    vector<int> findClosestElements
```\n//Approach-1 (Using priority queue : O((n+k)logk) (Time : 84ms)\nclass Solution {\npublic:\n    typedef pair<int, int> P;\n    vector<int> findClosestElements(vector<int>& arr, int k, int x) {\n        priority_queue<P, vector<P>> pq;\n        \n        for(int i = 0; i<arr.size(); i++) {\n            int diff = abs(x-arr[i]);\n            \n            pq.push({diff, arr[i]});\n            if(pq.size() > k)\n                pq.pop();\n        }\n        \n        vector<int> result;\n        while(!pq.empty()) {\n            result.push_back(pq.top().second);\n            pq.pop();\n        }\n        \n        sort(begin(result), end(result));\n        return result;\n    }\n};\n```\n\n```\n//Approach-2 (O(n)) Making use of sorted property (Not Binary Search) \n//(Time : 36ms)\nclass Solution {\npublic:\n    vector<int> findClosestElements(vector<int>& arr, int k, int x) {\n        int l = 0, r = arr.size()-1;\n        \n        while(r-l >= k) {\n            if(abs(arr[l]-x) <= abs(arr[r]-x))\n                r--;\n            else\n                l++;\n        }\n        \n        vector<int> result(arr.begin()+l, arr.begin()+l+k)\n        return result;\n    }\n};\n```\n\n```\n//Approach-3 (Using Binary Search ) O(log(n-k)) Time : 32ms\nclass Solution {\npublic:\n    vector<int> findClosestElements(vector<int>& arr, int k, int x) {\n        int l = 0;\n        int r = arr.size()-k; \n        /*\n            Why r = arr.size()-k?\n            Because, we will be comparing arr[mid] with arr[mid+k]\n            for moving l and r.\n            So, r should be arr.size()-k so that (mid+k) doesn\'t\n            go out of bound\n        */\n        \n\t\t/*\n\t\t\t\tThe tricky part of this solution is smart use of binary search.\n\t\t\t\tThe binary search here is used to find the best left\n\t\t\t\tindex (\'l\') rather than finding the closest element to x\n\t\t\t\tWe are actually in search of best \'l\' i.e. from where should\n\t\t\t\twe start to take k elements.\n\t\t*/\n        while(l < r) {\n            int mid = l + (r-l)/2;\n            /*\n                if arr[mid+k] is closer to x\n\t\t\t\tThere is no way \'l\' can start from mid.\n                so, move l = mid+1\n            */\n            \n            if(x - arr[mid] > arr[mid+k] - x)\n                l = mid+1;\n            else {\n                /*\n                    if arr[mid] is closer to x then:\n                    There are chances that we can start \'l\' from this mid\n\t\t\t\t\tand when this loop breaks (when l == r), so if r was \'mid\'\n\t\t\t\t\tthen \'l\' will be \'mid\' in the end too.\n                */\n                r = mid;\n            }\n        }\n        //We have found the best l to start with. Now, starting from l, we need k elements\n        vector<int> result(begin(arr)+l, begin(arr)+l+k);\n        return result;\n    }\n};\n```
[]