658. Find K Closest Elements - Detailed Explanation
Problem Statement
Description:
You are given a sorted integer array arr, and two integers k and x. Your task is to find the k closest integers to x in the array. The result should also be sorted in ascending order. An integer a is considered closer to x than an integer b if:
- |a - x| < |b - x|, or
- If |a - x| == |b - x|, thena < b.
It is guaranteed that the answer is unique. The k closest integers form a contiguous subarray of arr.
Examples:
- 
Example 1: - 
Input: arr = [1,2,3,4,5],k = 4,x = 3
- 
Output: [1,2,3,4]
- 
Explanation: The absolute differences with 3 are: 
 |1-3|=2, |2-3|=1, |3-3|=0, |4-3|=1, |5-3|=2.
 The four numbers with the smallest differences are 1, 2, 3, and 4.
 Although [2,3,4,5] also has the same differences for 2, 3, 4, and 5, when the absolute differences are equal (for 1 and 5), the smaller value is preferred.
 
- 
- 
Example 2: - 
Input: arr = [1,2,3,4,5],k = 4,x = -1
- 
Output: [1,2,3,4]
- 
Explanation: All elements are to the right of -1. The closest 4 elements are simply the first four numbers. 
 
- 
- 
Example 3: - 
Input: arr = [1,2,3,4,5],k = 4,x = 6
- 
Output: [2,3,4,5]
- 
Explanation: The absolute differences are: 
 |1-6|=5, |2-6|=4, |3-6|=3, |4-6|=2, |5-6|=1.
 The closest four are 2, 3, 4, and 5.
 
- 
Constraints:
- 
1 <= k <= arr.length
- 
1 <= arr.length <= 10^4
- 
arris sorted in ascending order.
- 
-10^4 <= arr[i], x <= 10^4
Hints
- 
Contiguous Subarray: 
 The problem guarantees that thekclosest numbers form a contiguous subarray. This observation allows you to consider sliding window or binary search techniques to find the correct window.
- 
Binary Search for the Window Start: 
 Instead of checking every possible subarray, you can perform binary search over the possible starting indices (from0toarr.length - k). For a candidate starting indexi, compare the distances betweenxandarr[i]versusxandarr[i+k]to decide whether to move the window left or right.
- 
Two-Pointer / Sliding Window: 
 Alternatively, you can use two pointers to shrink the window until its size becomesk, always removing the element (from either the left or right) that is farther fromx.
Multiple Approaches
Approach 1: Binary Search (Optimal)
Idea:
Since arr is sorted, the k closest elements form a contiguous subarray. We can binary search on the starting index of this subarray. Let left be the starting index and right be arr.length - k. For a given index mid in [left, right], compare the distance from x to arr[mid] with the distance from x to arr[mid+k]:
- If x - arr[mid] > arr[mid+k] - x, it means that the subarray starting atmidis too far left and we need to move right.
- Otherwise, move left.
Finally, the best starting index will yield the window of k elements that are closest to x.
Step-by-step:
- 
Initialize left = 0andright = arr.length - k.
- 
While left < right:- Let mid = left + (right - left) / 2.
- If x - arr[mid] > arr[mid+k] - x, then setleft = mid + 1; else, setright = mid.
 
- Let 
- 
Return arr[left ... left+k-1]as the answer.
Approach 2: Two-Pointer / Sliding Window
Idea:
Initialize two pointers at the start and end of the array and then narrow the window until its size equals k. At each step, compare the absolute difference between x and the elements at the ends of the current window, and remove the element that is farther away from x.
Step-by-step:
- 
Initialize two pointers: left = 0andright = arr.length - 1.
- 
While right - left + 1 > k:- If |arr[left] - x| > |arr[right] - x|, incrementleft(drop the left element).
- Otherwise, decrement right(drop the right element).
 
- If 
- 
Return the subarray from arr[left]toarr[right].
Note: Although the sliding window approach works well, the binary search approach is more efficient (especially when arr is large) with a time complexity of O(log(n-k) + k).
Step-by-Step Walkthrough (Binary Search Approach)
Let’s walk through an example with:
- arr = [1, 2, 3, 4, 5]
- k = 4
- x = 3
Step 1: Initialize the Search Boundaries
- Set left = 0andright = arr.length - k = 5 - 4 = 1.
Step 2: First Iteration of Binary Search
- 
Compute mid = 0 + (1 - 0) / 2 = 0.
- 
Compare distances: - |x - arr[mid]| = |3 - 1| = 2
- |arr[mid+k] - x| = |arr[0+4] - 3| = |5 - 3| = 2
 
- 
Since 2is not greater than2(i.e. the conditionx - arr[mid] > arr[mid+k] - xis false), we setright = mid = 0.
Step 3: End of Binary Search
- Now left == right == 0.
- The window starting at index 0is[1, 2, 3, 4].
Step 4: Return the Result
- The final answer is [1, 2, 3, 4].
Note: If x were closer to the right side, the binary search would adjust left accordingly.
Code Implementations
Python Implementation
Java Implementation
Complexity Analysis
- Time Complexity:
- Binary Search Approach: O(log(n - k)) for the binary search plus O(k) to output the subarray, which is very efficient.
- Two-Pointer/Sliding Window: O(n) in the worst case.
 
- Space Complexity:
- Both approaches use O(1) extra space (excluding the space for the output).
 
Common Mistakes
- 
Not using the sorted property: 
 A common pitfall is to generate all possible subarrays and then sort them by difference, which is inefficient.
- 
Incorrect Comparison: 
 When comparing distances, be careful with tie-breakers. If the absolute differences are equal, the smaller number should be preferred.
- 
Off-by-One Errors: 
 When using binary search, ensure the search boundaries are correctly set. The search should be performed over indices0toarr.length - k.
- 
Returning Non-Contiguous Subarray: 
 The answer must be a contiguous subarray from the original array.
Edge Cases
- 
When k equals the length of arr: 
 Simply return the whole array.
- 
When x is less than or equal to the first element: 
 The first k elements are the answer.
- 
When x is greater than or equal to the last element: 
 The last k elements are the answer.
- 
Tie in Differences: 
 When two elements are equally distant from x, the smaller element should be preferred (the binary search approach inherently handles this by comparingx - arr[mid]andarr[mid+k] - x).
Alternative Variations & Related Problems
- Closest Number in Sorted Array:
 Find the element closest to a target in a sorted array.
- Search Insert Position:
 Determine the index at which a target should be inserted in a sorted array.
- K Closest Points to Origin:
 A related problem that deals with finding the closest points based on Euclidean distance.
GET YOUR FREE
Coding Questions Catalog
$197

$78
$78