Easiest approach , use of only basic concepts TC=O(nlogn) SC=O(n)
Intuition\n\n1. Binary Search to Find the Closest Element\n2. Two-Pointer Technique to Find K-1 Closest \n3. Handling Edge Cases\n4. Sorting the Result \n\n---\
# Intuition\n\n1. Binary Search to Find the Closest Element\n2. Two-Pointer Technique to Find K-1 Closest \n3. Handling Edge Cases\n4. Sorting the Result \n\n---\n\n\n# Approach\n\n##### - The first part of the code uses binary search to find the element in the array arr that is closest to x. This element is considered as a candidate around which the k closest elements will be found.The binary search approach is employed because the array is sorted, allowing efficient lookup.The variable maybe is used to store the index of the element that is potentially the closest to x during the search. The algorithm compares the current element arr[mid] and updates maybe if a closer element is found\n\n##### - comparing maybe to next mid where \n`if |arr[mid]-x|>|arr[maybe]-x|` means still maybe is the closest to x. \n`if |arr[mid]-x|<|arr[maybe]-x|` mid is closer then maybe to x.\n`if |arr[mid]-x|==|arr[maybe]-x|`if both are equal then we have to choose which is smaller b/w these .\n\n##### - second part of this code is to get K-1 closest element , we take two pointer prev=maybe-1 and next=maybe+1 \n`if |arr[prev]-x|>|arr[next]-x|` means next is the closest . \n`if |arr[prev]-x|<|arr[next]-x|` means prev is closest .\n`if |arr[prev]-x|==|arr[prev]-x|` means prev is closest (as per given discription of Question )\n\n##### - at last we just have to sort our vector and return it\n\n---\n---\n\n\n\n# "This is just a basic idea of the approach. Please try to solve it after reading this. And if it helps, don\u2019t forget to upvote!"\n\n---\n---\n\n\n\n\n\n\n# Complexity\n- Time complexity:\n$$O(nlogn)$$ \n\n- Space complexity:\n$$O(n)$$ \n\n# Code\n```cpp []\nclass Solution {\npublic:\n    vector<int> findClosestElements(vector<int>& arr, int k, int x) {\n        int n=arr.size();\n        // predefine case\n        if(n==k)return arr;\n\n        vector<int>ans;\n\n        // find position of X in array\n        int lo=0,hi=n-1;\n        int maybe=lo+(hi-lo)/2;\n        while(lo<=hi){\n            int mid=lo+(hi-lo)/2;\n            if(arr[mid]>x){\n                if(abs(arr[maybe]-x)>abs(arr[mid]-x))maybe=mid;\n                else if(abs(arr[maybe]-x)==abs(arr[mid]-x))maybe=min(maybe,mid);\n                hi=mid-1;\n            }\n            else if(arr[mid]<x){\n                if(abs(arr[maybe]-x)>abs(arr[mid]-x))maybe=mid;\n                else if(abs(arr[maybe]-x)==abs(arr[mid]-x))maybe=min(maybe,mid);\n                lo=mid+1;\n            }\n            else if(arr[mid]==x){\n                maybe=mid;\n                break;\n            }\n        }\n\n        // if X present ho\n        ans.emplace_back(arr[maybe]);\n        k--;\n        int prev=maybe-1;\n        int next=maybe+1;\n        while(prev>=0 && next<n && k>0){\n            if(abs(arr[prev]-x)<=abs(arr[next]-x)){\n                ans.emplace_back(arr[prev]);\n                prev--;\n                k--;\n            }\n            else if(abs(arr[prev]-x)>abs(arr[next]-x)){\n                ans.emplace_back(arr[next]);\n                next++;\n                k--;\n            }\n        }\n        if(k>0 && prev==-1){\n            while(k>0){\n                ans.emplace_back(arr[next]);\n                next++;\n                k--;\n            }\n        }\n        else if(k>0 && next==n){\n            while(k>0){\n                ans.emplace_back(arr[prev]);\n                prev--;\n                k--;\n            }\n        }\n        \n        sort(ans.begin(),ans.end());\n        return ans;\n    }\n};\n```
['Two Pointers', 'Binary Search', 'C++']