-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefuse_the_bomb.cpp
64 lines (61 loc) · 1.53 KB
/
Defuse_the_bomb.cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<bits/stdc++.h>
using namespace std;
// vector<int> defuseTheBomb(vector<int>& code, int k) {
// int n = code.size();
// vector<int> res(n, 0);
// if(k == 0){
// return res;
// }
// for(int i = 0; i < n; i++){
// if(k > 0){
// for(int j = 1; j <= k; j++){
// res[i] += code[(i+j)%n];
// }
// }else{
// for(int j = 1; j <= abs(k); j++){
// res[i] += code[(i-j+n)%n];
// }
// }
// }
// return res;
// }
vector<int> defuseTheBomb(vector<int>& arr, int k) {
int n = arr.size();
vector<int> result(n, 0);
if(k == 0){
return result;
}else if(k>0){
for(int i = 0; i<n; i++){
int sum = 0;
int j = i+1;
int count = k;
while(count--){
sum += arr[j%n];
j++;
}
result[i] = sum;
}
}else{
for(int i = 0; i<n; i++){
int sum = 0;
int j = i-1;
int count = -k;
while(count--){
sum += arr[(j+n)%n];
j--;
}
result[i] = sum;
}
}
return result;
}
// main function
int main(){
vector<int> code = {5,7,1,4};
int k = 3;
vector<int> res = defuseTheBomb(code, k);
for(auto it:res){
cout<<it<<" ";
}
return 0;
}