[Python] Binary search + two pointers, explained
Let us first solve given problem when k = 1, that is we need to find closest element in our tree. For this we can use binary search and then check two possible 
Let us first solve given problem when `k = 1`, that is we need to find closest element in our tree. For this we can use binary search and then check two possible options for element just before `x` and for element just after `x`. Once we found the closest number, we can have two possible options: either the next closest number is smaller or bigger, which one to choose? We check the distance to both of them and choose the closest one. On the next step we also have two choices and so on. So, what we have in the end is two pointers approach and on each step we decide which element we take.\n\n#### Complexity\nTime complexity is `O(log n + k)`, space complexity is `O(k)`.\n\n#### Code\n```python\nfrom bisect import bisect, bisect_left\n\nclass Solution:\n    def findClosestElements(self, arr, k, x):\n        n = len(arr)\n        ind = bisect_left(arr, x)\n        if ind == n or ind > 0 and arr[ind] + arr[ind-1] >= 2*x:\n            ind -= 1\n\n        beg, end = ind, ind\n        for i in range(k-1):\n            if beg == 0: end += 1\n            elif end == n-1: beg -= 1\n            else:\n                if arr[end+1] + arr[beg-1] >= 2*x:\n                    beg -= 1\n                else:\n                    end += 1\n\n        return arr[beg:end+1]\n```\n\nIf you have any questions, feel free to ask. If you like solution and explanations, please **Upvote!**
['Two Pointers', 'Binary Search']