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 < barr is sorted in ascending order.n) and the integers k and x can vary within certain constraints typically found in competitive programming (e.g., 1 <= k <= n and 1 <= n <= 10^4 or 10^5).1 <= k <= n. Hence, this situation should not occur.To find the k closest integers to x, we can use a binary search combined with a two-pointer technique:
x in the array.k closest elements.Here’s the step-by-step solution:
bisect_left to find the index where x would be inserted to maintain the sorted order.k elements.Here’s the Python implementation of the above strategy:
from bisect import bisect_left
def findClosestElements(arr, k, x):
# Find the index where `x` would be inserted to keep the sorted order
position = bisect_left(arr, x)
# Initialize two pointers
left = position - 1
right = position
# While we need to find k elements
while k > 0:
# If left pointer is out of bounds, move right pointer
if left < 0:
right += 1
# If right pointer is out of bounds, move left pointer
elif right >= len(arr):
left -= 1
# Compare the distance of two pointers to x
elif abs(arr[left] - x) <= abs(arr[right] - x):
left -= 1
else:
right += 1
# Decrease k since we have selected one element
k -= 1
# Return the sub-array from left+1 to right (exclusive)
return arr[left + 1:right]
# Example usage
arr = [1, 2, 3, 4, 5]
k = 4
x = 3
print(findClosestElements(arr, k, x)) # Output: [1, 2, 3, 4]
The time complexity of this algorithm is:
O(log n)O(k)Overall, the time complexity is O(log n + k). Given the constraints, this approach is efficient and should perform well for large inputs.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?