Back to problems Sign in to unlock
Leetcode
Medium
Arrays
Hash Maps
Find All K Distant Indices In An Array
Return every array index whose distance to any occurrence of a given key is at most k.
Acceptance 100%
Problem Statement
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.
Input Format
- An integer array
nums - An integer
key - An integer
k
Output Format
- A list of all valid indices in increasing order
Constraints
1 <= nums.length0 <= knums[i]andkeyare integers- All returned indices must satisfy the distance condition for at least one occurrence of
key
Examples
Sample cases returned by the problem API.
Example 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
Unlock deeper context for this problem
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.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.