2320. Find All K-Distant Indices in an Array
My accepted Python solution to LeetCode problem 2320, Find All K-Distant Indices in an Array, running in 179ms.
- Difficulty: Easy
- Python
- Runtime 179ms
- Memory 17.8MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 179ms, memory 17.8MB, accepted 2025-12-29.
class Solution:
def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:
# Find all indices j where nums[j] == key
key_indices = [j for j in range(len(nums)) if nums[j] == key]
# Find all k-distant indices
result = set()
for j in key_indices:
for i in range(max(0, j - k), min(len(nums), j + k + 1)):
result.add(i)
return sorted(result)