Amazon VO Coding Interview: Find the K Closest Elements in a Sorted Array

20 Views
No Comments

Given a sorted array arr = [1, 2, 3, 10, 11, 12], find k = 3 closest elements around target m.

This problem asks for the k elements in a sorted array that are closest to a target m. Since the array is already ordered, the key idea is to exploit that structure: either locate the target region with binary search and expand using two pointers, or maintain a sliding window of size k and compare boundary distances to decide which side to drop. The main insight is turning an otherwise brute-force search into an efficient solution by using the sorted order.

END
 0