Binary search solution using the left + 1 < right template
While most posts use "left < right" as the while condition, my solution uses "left + 1 < right". It is essentially the same as the most popular solution, where 
While most posts use "left < right" as the while condition, my solution uses "left + 1 < right". It is essentially the same as the most popular solution, where you compare x with (arr[mid]+arr[mid+k])/2 to decide to which half to discard. I am posting this for fans of the "left + 1 < right" template.\n\n**Pros:**\nNo need to worry about mid/mid+1/mid-1.\n\n**Cons:**\nThis template returns a window of size 2, you need examine left and right to determine which one to output as answer.\n\n```\nclass Solution:\n    def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:\n        # edge cases\n        if k == len(arr):\n            return arr\n        \n        if x <= arr[0]: \n            return arr[:k]\n\t\t\t\n        if x >= arr[-1]: \n            return arr[-k:]\n\n\t\t# the only difference between arr[mid:mid+k] and arr[mid+1:mid+k+1] is arr[mid] and arr[mid+k]\n\t\t# compare x with (arr[mid]+arr[mid+k])/2 to decide to which half to discard\t\n        \n        left, right = 0, len(arr) - k\n        while left + 1 < right:\n            mid = (left + right) // 2\n            if x <= (arr[mid] + arr[mid+k]) / 2:\n                right = mid\n            else:\n                left = mid\n\n\t\t# Similarly, compare x with (arr[left] + arr[right+k-1])/2 to decide use left or right\n        if x <= (arr[left] + arr[right+k-1]) / 2:\n            return arr[left:left+k]\n        else:\n            return arr[right:right+k]\n```\n\n\n
['Binary Tree', 'Python']