Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions 2200. Find All K-Distant Indices in an Array
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
vector<int> findKDistantIndices(vector<int>& nums, int key, int k)
{
vector<int> ans;
int n = nums.size(), last_index = 0;
for(int j = 0; j < n; j++)
{
if(nums[j] == key)
{
int start = max(last_index, j - k);
int end = min(j + k + 1, n);
for(int i = start; i < end; i++)
ans.push_back(i);
last_index = end;
}
}
return ans;
}
};
Loading