-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2200.cpp
More file actions
45 lines (34 loc) · 965 Bytes
/
2200.cpp
File metadata and controls
45 lines (34 loc) · 965 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
std::vector<int>findKDistantIndices(std::vector<int>& nums, int key, int k) {
std::vector<int> indices;
std::vector<int> keys;
for(int i = 0; i < nums.size(); ++i) {
if(nums[i] == key) {
keys.push_back(i);
}
}
for(int i = 0; i < nums.size(); ++i) {
for(int j = 0; j < keys.size(); ++j) {
if(abs(i - keys[j]) <= k) {
indices.push_back(i);
}
}
}
std::sort(indices.begin(), indices.end());
indices.erase(std::unique(indices.begin(), indices.end()), indices.end());
return indices;
}
int main() {
// std::vector<int> nums = { 3,4,9,1,3,9,5 };
std::vector<int> nums = { 2,2,2,2,2 };
int key = 2;
int k = 2;
std::vector<int> vec = findKDistantIndices(nums, key, k);
for(int x : vec) {
std::cout << x << " ";
}
std::cin.get();
}