Closest k elements to x
Your task
Given a sorted ascending integer array arr, an integer k, and an integer x, return the k elements closest to x, in ascending order. Closeness ties break toward the smaller value (|a-x| < |b-x|, or equal and a < b). 1 <= k <= len(arr). Aim for O(log n + k): binary-search for the best left boundary of the size-k window rather than sorting by distance.
Examples
find_closest_elements(arr: list[int], k: int, x: int) -> list[int]Example 1
Input
find_closest_elements([1, 2, 3, 4, 5], 4, 3)Output
[1, 2, 3, 4]