CPP| Binary search+ Merge, Time: O(log(N) + k*log(k)), Space: O(k)| comments| explanation
Way of thinking:-\n1) First think of finding the closest element to x in arr.\n2) Now you have to find the other k-1 elements from either left or right from the
**Way of thinking:-**\n1) First think of finding the closest element to **x** in ***arr***.\n2) Now you have to find the other **k-1** elements from either left or right from the closest element which are having least difference from x.\n\n**Approach:-**\n1) For finding the closest element, the most optimal approach will be binary search\n2) Now as you have got the closest **index** from **x**, you will include that arr[index] in the answer vector.\n3) Now you have to take a pointer ***i**=index-1* and ***j**=index+1*. \n4) Now, comparing between **i** and **j**, which ever is smaller, we will put that into answer vector, and update the values of i and j.\n5) This we will do for **k-1** times.\n6) The last step is to sort the **answer** vector and return.\n\nTime complexity: **O(log(N) + k\\*log(k))**\nBinary search-O(log(N)) \nmerging k elements- O(k\\*log(k))\n\nSpace complexity: **O(k)**, for the answer vector\n\n```\nclass Solution {\npublic:\n    vector<int> findClosestElements(vector<int>& arr, int k, int x) {\n        int n=arr.size();\n        int l=0,h=n-1,mid=0;\n       // binary search part for getting closest to the element x\n        while(l<=h)\n        {\n            mid=(l+h)/2;\n            if(arr[mid]==x)\n                break;\n            else if(x<arr[mid])\n                h=mid-1;\n            else\n                l=mid+1;\n        }\n        // this confirms the actual closest element from x\n        // if the diff is coming same, then we are giving priority to left elements, as in sorted \n        // order they come first.\n        int index=mid;\n        if(mid-1>=0 && (abs(arr[mid-1]-x)<=abs(arr[index]-x)))\n            index=mid-1;\n        if(mid+1<n && (abs(arr[mid+1]-x)<abs(arr[index]-x)))\n            index=mid+1;\n       \n        // this is taken from merging two sorted array algorithm.\n        // selecting the closest index and setting the pointers to its right=j and left=i\n        //and then traversing i to the left and j to the right based on their sorted values\n        // k times, at the last sort the ans vector and return that\n        int i=index-1;\n        int j=index+1;\n        vector<int>answer;\n        answer.push_back(arr[index]);\n        k--;\n        while(k>0 && i>=0 && j<n)\n        {\n            if(abs(arr[i]-x)<=abs(arr[j]-x))  // here also giving more priority to i than j.\n            {\n                answer.push_back(arr[i]);\n                k--;\n                i--;    // moving i to the left\n            }\n            else\n            {\n                answer.push_back(arr[j]);\n                k--;\n                j++;  // moving j to the right\n            }\n        }\n        while(i>=0 && k>0)\n        {\n            answer.push_back(arr[i]);\n            k--;\n            i--;\n        }\n        while(j<n && k>0)\n        {\n            answer.push_back(arr[j]);\n            k--;\n            j++;\n        }\n      // merge part ends here\n\t  \n         sort(answer.begin(),answer.end()); // sorting the answer vector before returning\n        return answer;\n    }\n};\n```
['Binary Search', 'C', 'Binary Tree', 'C++']