12. K Closest Elements
โ GFG solution to the K Closest Elements problem: find k closest elements to target x in sorted array using binary search and two pointers. ๐
๐งฉ Problem Description
๐ Examples
Example 1
Input: arr[] = [1, 3, 4, 10, 12], k = 2, x = 4
Output: 3 1
Explanation: 4 is excluded. Closest elements to 4 are:
- 3 (distance = 1)
- 1 (distance = 3)
So, the 2 closest elements are: 3 1Example 2
๐ Constraints
โ
My Approach
Binary Search + Two Pointers
๐ Time and Auxiliary Space Complexity
๐งโ๐ป Code (C++)
๐งโ๐ป Code (Java)
๐ Code (Python)
๐ง Contribution and Support
๐Visitor Count
Last updated