Python O(log(n) +k) solution using bisect and two pointers
There are faster way to do this question in other post: https://leetcode.com/problems/find-k-closest-elements/discuss/106426/JavaC++Python-Binary-Research-O(log
There are faster way to do this question in other post: https://leetcode.com/problems/find-k-closest-elements/discuss/106426/JavaC++Python-Binary-Research-O(log(N-K))\nBut I\'d like to give a Python specific solution since it has a good tool in bisect module. Basic idea is using bisect_left find the idx(or provided idx) in the arr, the candidates for closes k would only be in left k and right k elements of this idx. Then just simple do a two pointer to find right index range.\n```\n    def findClosestElements(self, arr, k, x):\n        """\n        :type arr: List[int]\n        :type k: int\n        :type x: int\n        :rtype: List[int]\n        """\n        idx = bisect.bisect_left(arr, x) #O(log(n))\n        \n        left, right = max(0, idx - k), min(idx + k, len(arr) - 1)\n        #O(k)\n        while right - left + 1 > k: #more than k element in the range\n            if x - arr[left] > arr[right] - x: #remove left candidate\n                left += 1\n            else:\n                right -= 1\n        return arr[left: right + 1]\n```
[]