Skip to content

Commit eb63f34

Browse files
committed
feat: easy: find-all-k-distant-indices-in-an-array.go
1 parent 8cb94c3 commit eb63f34

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package easy
2+
3+
func findKDistantIndices(nums []int, key, k int) []int {
4+
n := len(nums)
5+
mark := make([]bool, n)
6+
7+
// mark every index that lies in a key-window
8+
for i, v := range nums {
9+
if v == key {
10+
left, right := max(0, i-k), min(n-1, i+k)
11+
for j := left; j <= right; j++ {
12+
mark[j] = true
13+
}
14+
}
15+
}
16+
17+
// collect marked indices in ascending order
18+
out := make([]int, 0)
19+
for i, ok := range mark {
20+
if ok {
21+
out = append(out, i)
22+
}
23+
}
24+
return out
25+
}

0 commit comments

Comments
 (0)