Return every array index whose distance to any occurrence of a given key is at most k.
Given an integer array nums, an integer key, and an integer k, find all indices i such that there exists at least one index j with nums[j] == key and |i - j| <= k.
Return the indices in increasing order.
The task is to identify all positions that are within distance k of any occurrence of key in the array.
numskeyk1 <= nums.length0 <= knums[i] and key are integerskeyExample 1
Input
nums = [3,4,9,1,3,9,5], key = 9, k = 1
Output
[1,2,3,5,6]
Explanation
Indices 1, 2, and 3 are within distance 1 of the key at index 2. Indices 5 and 6 are within distance 1 of the key at index 5.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.