-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryDequeue.cpp
47 lines (42 loc) · 1.17 KB
/
BinaryDequeue.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
/*
Problem statement:
Slavic has an array of length n consisting only of zeroes and ones. In one operation, he removes either the first or the last element of the array.
What is the minimum number of operations Slavic has to perform such that the total sum of the array is equal to s after performing all the operations?
In case the sum s can't be obtained after any amount of operations, you should output -1.
*/
#include <iostream>
#include<vector>
using namespace std;
int main(void) {
int T;
cin >> T;
while (T--) {
int n, k,input;
cin >> n >> k;
vector<int>v;
int sum = 0;
for (int i = 0; i < n; i++) {
cin >> input;
v.push_back(input);
sum += v[i];
}
if (sum < k) {
cout << -1 << endl;
continue;
}
int l = 0, tempSum = 0, startIndex = 0;
for(int i=0;i<n;i++) {
//increasing i untill you get extra 1 in your sum.
tempSum += v[i];
//terminate loop until your sum becomes K.
while (temp > k) {
tempSum = tempSum - v[startIndex];
startIndex++;
}
//finding maximum gap/length between i and startIndex having sum k.
l = max(l, i - startIndex + 1);
}
cout << n - l << endl;
}
return 0;
}