↓Skip to main content
  1. LeetCode/

LeetCode 2200: Find All K-Distant Indices in an Array

·2 mins· ·
LeetCode Easy Two-Pointers Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2026-01-04

  • Submissions: Accepted (brute force) → Wrong Answer → Accepted ×2

Approach
#

Every position with nums[j] == key makes each index in [j - k, j + k] part of the answer. Output the union of these ranges in order.

Scanning key positions in order, range starts only move right, so a pointer next_start remembers how far output has gone: the next range starts at max(next_start, j - k) and nothing repeats.

My Attempts
#

Attempt 1: brute force
#

For every index, check all key positions and include it if any is within k. Correct, in O(n · number of keys) time.

Attempt 2: overlapping ranges emitted twice
#

The next version emitted [j - k, j + k] for every key without handling overlaps. For nums = [2, 3, 2, 1, 1, 2, 1, 2], key = 2, k = 3, every key’s range overlaps the others, so indices were emitted several times: [0, 1, 2, 3, 0, 1, 2, 3, 4, 5, ...].

Adding a pointer to the end of the previous range passed.

Solution
#

class Solution:
    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:
        n = len(nums)
        res = []
        next_start = 0                         # first index not yet emitted
        for j, x in enumerate(nums):
            if x == key:
                start = max(next_start, j - k)
                end = min(n - 1, j + k)
                res.extend(range(start, end + 1))
                next_start = end + 1
        return res

Complexity
#

Time O(n): each index is emitted at most once. Space O(1) excluding the output.

Mistakes to Avoid
#

  • Emitting overlapping ranges twice.
  • Not clamping ranges to [0, n - 1].

Interview Explanation
#

Each position j holding key makes [j - k, j + k] part of the answer. I scan key positions in order with a pointer to how far output has gone, starting each range at max(pointer, j - k), so overlaps are never emitted twice. Each index is output at most once, so time is O(n).

Related

LeetCode 1089: Duplicate Zeros
·2 mins
LeetCode Easy Array Two-Pointers
LeetCode 1089 note: count the zeros to know each value’s shift, then move from back to front, writing the extra 0 when copying a zero
LeetCode 977: Squares of a Sorted Array
·1 min
LeetCode Easy Array Two-Pointers Sorting
LeetCode 977 note: the largest squares are at the two ends, so compare the ends with two pointers and fill the result from the back
LeetCode 905: Sort Array By Parity
·1 min
LeetCode Easy Array Two-Pointers Sorting
LeetCode 905 note: a write pointer swaps even numbers to the front, just like move zeroes
LeetCode 922: Sort Array By Parity II
·1 min
LeetCode Easy Array Two-Pointers Sorting
LeetCode 922 note: pointers i over even slots and j over odd slots, each stepping by two; when slot i holds an odd number, move j to a misplaced even and swap
LeetCode 821: Shortest Distance to a Character
·2 mins
LeetCode Easy Array Two-Pointers String
LeetCode 821 note: one left-to-right pass for the distance to the nearest c on the left, then a right-to-left pass taking the minimum
LeetCode 832: Flipping an Image
·2 mins
LeetCode Easy Array Two-Pointers Bit-Manipulation Matrix
LeetCode 832 note: reverse each row and invert its bits, which two pointers can do at once