2200. Find All K-Distant Indices in an Array

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2200

class Solution:
    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:
        res = []
        j, n = 0, len(nums)
        for i in range(n):
            while j < i - k or (j < n and nums[j] != key):
                j += 1
            if j < n and j <= (i + k):
                res.append(i)
        return res