Skip to content

2200. Find All K-Distant Indices in an Array #1846

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to find all k-distant indices in an array. A k-distant index i is defined as an index where there exists at least one index j such that the absolute difference between i and j is at most k and the value at j is equal to the given key.

Approach

  1. Problem Analysis: The task involves identifying all indices i in the array that are within a distance k from any index j where nums[j] equals key. The solution requires efficiently marking all such indices i without duplicates and returning them in sorted order.

  2. Intuition: For each occurrence of key in the array, we can determine the range of indices [j - k, j + k] that are k-distant. To efficiently mark these indices, we use a line swee…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Jun 24, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants