diff --git a/2200. Find All K-Distant Indices in an Array b/2200. Find All K-Distant Indices in an Array new file mode 100644 index 0000000..dff73b6 --- /dev/null +++ b/2200. Find All K-Distant Indices in an Array @@ -0,0 +1,20 @@ +class Solution { +public: + vector findKDistantIndices(vector& nums, int key, int k) + { + vector 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; + } +};