Python O(log(N-K) + K) Binary Search W left <= right
This is derived from Lee\'s code here: https://leetcode.com/problems/find-k-closest-elements/discuss/106426/JavaC%2B%2BPython-Binary-Search-O(log(N-K)-%2B-K) \n
This is derived from Lee\'s code here: https://leetcode.com/problems/find-k-closest-elements/discuss/106426/JavaC%2B%2BPython-Binary-Search-O(log(N-K)-%2B-K) \n\nIt always confuses me when we do left < right, so I just rewrote the function to be left <= right. \n\nI see a lot of binary search templates and here are a few points for "translations" across the templates. \nThe left side is a left <= right template, the right side is a left < right template\n1). left <= right vs left < right\n2). right = mid -1 vs right = mid\n3). right = len(arr) - k - 1 vs right = len(arr) - k\n\nFor me it is much simpler to remember the left <= right template. Maybe it is different for you. \n\n```\ndef findClosestElements(self, arr, k, x):\n        if len(arr) <= k: return arr\n        left, right = 0, len(arr) - k - 1\n        while left <= right: \n            mid = left + (right - left)/2\n            if x - arr[mid] > arr[mid + k] - x:\n                left = mid + 1\n            else: \n                right = mid - 1\n        return arr[left:left+k]\n```
[]