You are given a sorted integer array arr, two integers k and x, return the k closest integers to x in the array. The result should also be sorted in ascending order.
An integer a is closer to x than an integer b if:
|a - x| < |b - x|, or|a - x| == |b - x| and a < bExample 1:
Input: arr = [2,4,5,8], k = 2, x = 6
Output: [4,5]Example 2:
Input: arr = [2,3,4], k = 3, x = 1
Output: [2,3,4]Constraints:
1 <= k <= arr.length <= 10,000.-10,000 <= arr[i], x <= 10,000arr is sorted in ascending order.The problem asks for the k elements closest to x. A straightforward approach is to sort all elements by their distance to x. If two elements have the same distance, we prefer the smaller one. After sorting, we simply take the first k elements and return them in sorted order.
x. If two elements have the same distance, the smaller element comes first.k elements from the sorted array.k elements in ascending order (since the output must be sorted).Where is the size of the input array and is the number of closest elements to find.
Since we need elements closest to x, we can first find the element nearest to x using a linear scan. Once we have this starting point, we expand outward using two pointers, picking the closer element at each step until we have k elements.
x.l pointing to the index before the closest element, and r pointing to the index after.k elements:class Solution:
def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
n = len(arr)
idx = 0
for i in range(1, n):
if abs(x - arr[idx]) > abs(x - arr[i]):
idx = i
res = [arr[idx]]
l, r = idx - 1, idx + 1
while len(res) < k:
if l >= 0 and r < n:
if abs(x - arr[l]) <= abs(x - arr[r]):
res.append(arr[l])
l -= 1
else:
res.append(arr[r])
r += 1
elif l >= 0:
res.append(arr[l])
l -= 1
elif r < n:
res.append(arr[r])
r += 1
return sorted(res)Where is the size of the input array and is the number of closest elements to find.
Since the array is already sorted, the k closest elements must form a contiguous subarray. We can use two pointers starting at the ends of the array and shrink the window by removing the element that is farther from x until only k elements remain.
l = 0 and r = n - 1.r - l >= k:arr[l] and arr[r] from x.r inward.l to r (inclusive).Where is the size of the input array and is the number of closest elements to find.
Instead of scanning linearly to find the starting point, we can use binary search to quickly locate where x would fit in the sorted array. From that position, we expand outward with two pointers to collect k closest elements.
arr[mid] >= x.l pointing one position to the left of this index, and r at this index.k elements (i.e., r - l - 1 < k):l < 0, expand right.r >= n, expand left.l + 1 to r - 1 (exclusive bounds).class Solution:
def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
l, r = 0, len(arr) - 1
while l < r:
mid = (l + r) // 2
if arr[mid] < x:
l = mid + 1
else:
r = mid
l, r = l - 1, l
while r - l - 1 < k:
if l < 0:
r += 1
elif r >= len(arr):
l -= 1
elif abs(arr[l] - x) <= abs(arr[r] - x):
l -= 1
else:
r += 1
return arr[l + 1:r]Where is the size of the input array and is the number of closest elements to find.
We can binary search directly for the starting index of the k-length window. For any starting index m, we compare the distances of arr[m] and arr[m + k] to x. If arr[m + k] is closer, the window should shift right; otherwise, it should stay or shift left. This narrows down the optimal starting position.
l = 0 and r = n - k (the rightmost valid starting index).l < r:m = (l + r) / 2.x - arr[m] with arr[m + k] - x.x - arr[m] > arr[m + k] - x, the window is too far left, so set l = m + 1.r = m.l with length k.Where is the size of the input array and is the number of closest elements to find.
When two elements have the same distance to x, the problem requires preferring the smaller element. A common mistake is treating equal distances arbitrarily or preferring the larger element. This affects both the custom comparator approach and the two-pointer shrinking logic where you should shrink from the right (larger values) when distances are equal.
The output must be sorted in ascending order. When using approaches that collect elements based on distance (like sorting by distance or expanding from a center point), the collected elements may not be in sorted order. Forgetting to sort the final result before returning leads to incorrect output.
In the optimized binary search approach, the search range should be [0, n - k] for the starting index of the window, not [0, n - 1]. Using incorrect bounds can cause the window to extend past the array end or miss valid starting positions, leading to index out of bounds errors or wrong answers.