res to store the k-distant indices.j to 0 to keep track of the last index added to res.nums using pointer i.nums[i] equals key, perform the following steps:
a. Update j to be the maximum of j and i - k.
b. While j is less than or equal to i + k and less than the length of nums, add j to res and increment j.res.