Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 72ce4fc

Browse files
authored
Create Find All K-Distant Indices in an Array.java
1 parent 8e0759d commit 72ce4fc

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public List<Integer> findKDistantIndices(int[] nums, int key, int k) {
3+
List<Integer> result = new ArrayList<>();
4+
for (int i = 0; i < nums.length; i++) {
5+
if (nums[i] == key) {
6+
int start = Math.max(result.size() == 0 ? 0 : result.get(result.size() - 1) + 1, i - k);
7+
for (int j = start; j <= i + k && j < nums.length; j++) {
8+
result.add(j);
9+
}
10+
}
11+
}
12+
return result;
13+
}
14+
}

0 commit comments

Comments
 (0)