-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefuseBomb.cpp
More file actions
52 lines (44 loc) · 1.5 KB
/
DefuseBomb.cpp
File metadata and controls
52 lines (44 loc) · 1.5 KB
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
/*
A war is happening. The enemy battalion has planted a bomb in your bunker. Your spy has intercepted
a message from the enemy. It contains a list with N numbers and a key (K). The numbers are used to
construct a sequence that will defuse the bomb.
According to your spy, the logic to extract the sequence from the message is to replace each number
with the sum of the next K numbers, if the value of K is positive. If the value of K is negative,
the number is replaced by the sum of the previous K numbers. The series of numbers is considered in
a cyclic fashion for the last K
numbers.
Input Format:
The input to the function/method consists of three arguments; size, an integer representing the
size of the list (N);
key, an integer representing the key (K); message, representing the list of integers.
Output Format:
Return a list of integers representing the sequence that will defuse the bomb
Constraints:
0 < size <= 10^5
-10^6 <= message[i] <= 10^6 0 <= i < size
Sample Input : 4 3 4 2 -5 11
Sample Output :
8 10 17 1
*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int>arr={1,2,3,4},res;
int k=2,i=0,cnt=0,sum=0,idx=0;
while(i<arr.size()){
idx=i,cnt=0,sum=0;
while(cnt<k){
if(idx==arr.size()-1) idx=0;
else idx++;
if(arr[idx]>0){
sum+=arr[idx];
cnt++;
}
}
res.push_back(sum);
i++;
}
for(int item:res) cout<<item<<" ";
return 0;
}