-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubarray.cpp
53 lines (44 loc) · 1.18 KB
/
subarray.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
#include <iostream>
#include <vector>
using namespace std;
int main() {
int t;
cin >> t;
bool foundanswer = false;
while(t--) {
int n, s;
cin >> n >> s;
//cout << "N and S are " << n << " " << s << endl;
int sum = 0;
int startindex = 0;
vector<int> values;
for (int i=0; i < n; i++) {
int temp;
cin >> temp;
values.push_back(temp);
}
for(int i=startindex; i < n; i++) {
sum = sum + values[i];
//cout << "Value : " << values[i] << endl;
if (sum > s) {
startindex++;
//cout << "Resetting... " << sum << endl;
i = startindex - 1;
sum = 0;
}
if (sum == s) {
//cout << "Found." << endl;
//for(int j=startindex; j<=i; j++) {
// cout << values[j] << " ";
//}
cout << startindex+1 << " " << i+1 << endl;
foundanswer = true;
break;
}
}
if (foundanswer == false) {
cout << -1 << endl;
}
}
return 0;
}