C++, Binary Search, two solutions
Solution1: 80ms\n\nstatic int fast_io = []() { std::ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();\n\nclass Solution {\npublic:\n    vector<int> 
**Solution1:** 80ms\n```\nstatic int fast_io = []() { std::ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();\n\nclass Solution {\npublic:\n    vector<int> findClosestElements(vector<int>& A, int k, int x) {\n        // two facts are clear since array A is sorted\n        // 1. If x is smaller than A[0], then k elements start from x\n        // 2. If x is greater than A[N-1] then result will be last k elements\n        int N = A.size();\n        if(N==0) return {};\n        if(N<k) return {};\n        if(x<=A[0]) return vector<int>(A.begin(),A.begin()+k);\n        if(x>=A[N-1]) return vector<int>(A.begin()+N-k,A.end());\n\t\t\n        int lo = 0;\n        int hi = N-1;\n        while(lo<=hi){\n            int mid = lo + (hi-lo)/2;\n            if(x==A[mid]) {lo=mid;break;}\n            else if(x>A[mid]) lo =mid+1;\n            else hi = mid-1;\n        }\n        // lo gives the correct position\n        vector<int> ans;\n        int index = lo;\n        int left = index-1;\n        int right = index;\n        cout << A[left] << " " << A[right] << "\\n";\n        while(k>0){\n            if(left>=0 && right<=N-1){\n                if((A[right]-x)>=(x-A[left])){\n                    ans.push_back(A[left]); // insert left\n                    left--;\n                }\n                else{\n                    ans.push_back(A[right]);\n                    right++;    \n                }\n            }\n            else if(right<=N-1){\n                ans.push_back(A[right]);\n                right++;    \n            }\n            else if(left>=0){\n                ans.push_back(A[left]); // insert left\n                left--;\n            }\n            k--;\n        }\n        sort(ans.begin(),ans.end());\n        return ans;\n    }\n};\n```\n\n**One more approach:** 76ms\n\n```\nclass Solution {\npublic:\n    vector<int> findClosestElements(vector<int>& A, int k, int x) {\n        int N = A.size(), lo = 0, hi = N - 1;\n        while (hi - lo + 1 > k)\n            if (abs(A[hi] - x) >= abs(A[lo] - x))\n                hi--;\n            else\n                lo++;\n        vector<int> ans(A.begin() + lo, A.begin() + hi + 1);\n        return ans;\n    }\n};\n```
['C', 'Binary Tree']