In the Find All K-Distant Indices in an Array coding problem, you are given an array nums, a target value, and an integer k. An index i is "k-distant" if there exists any index j such that nums[j] == target and the absolute distance . You need to return all such "good" indices in increasing order.
This "Easy" difficulty question is used by Meta and Amazon to test a candidate's ability to handle range overlaps efficiently. While a brute-force check is possible, it's not optimal. It evaluation whether you can identify the Two Pointers interview pattern or a "Sliding Window" of impact to solve the problem in a single linear pass.
This is a Linear Scan and Range Merging problem.
j where nums[j] == target.j makes the range valid.last_covered to keep track of the largest index added to the result to avoid duplicates and ensure the output is sorted.nums = [3, 4, 9, 1, 3, 9, 5], target = 9, k = 1.
last_covered = 3.last_covered < 4, start adding from 4. Add 4, 5, 6 to result.
Result: [1, 2, 3, 4, 5, 6].When you have multiple overlapping "regions of influence" (like the -distance here), avoid a nested loop. Instead, track the "frontier" of your progress. This is a common theme in Interval interview patterns.