Skip to content

Commit 2cef8fd

Browse files
Sridhar VenkatSridhar Venkat
Sridhar Venkat
authored and
Sridhar Venkat
committed
Initial commit
0 parents  commit 2cef8fd

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# subarraysum

input1.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
3
2+
74 665
3+
142 112 54 69 148 45 63 158 38 60 124 142 130 179 117 36 191 43 89 107 41 143 65 49 47 6 91 130 171 151 7 102 194 149 30 24 85 155 157 41 167 177 132 109 145 40 27 124 138 139 119 83 130 142 34 116 40 59 105 131 178 107 74 187 22 146 125 73 71 30 178 174 98 113
4+
5 12
5+
1 2 3 7 5
6+
10 15
7+
1 2 3 4 5 6 7 8 9 10
8+

subarray.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
int main() {
7+
int t;
8+
cin >> t;
9+
bool foundanswer = false;
10+
11+
while(t--) {
12+
int n, s;
13+
cin >> n >> s;
14+
//cout << "N and S are " << n << " " << s << endl;
15+
16+
int sum = 0;
17+
int startindex = 0;
18+
vector<int> values;
19+
20+
for (int i=0; i < n; i++) {
21+
int temp;
22+
cin >> temp;
23+
values.push_back(temp);
24+
}
25+
26+
for(int i=startindex; i < n; i++) {
27+
sum = sum + values[i];
28+
//cout << "Value : " << values[i] << endl;
29+
30+
if (sum > s) {
31+
startindex++;
32+
//cout << "Resetting... " << sum << endl;
33+
i = startindex - 1;
34+
sum = 0;
35+
}
36+
37+
if (sum == s) {
38+
//cout << "Found." << endl;
39+
//for(int j=startindex; j<=i; j++) {
40+
// cout << values[j] << " ";
41+
//}
42+
cout << startindex+1 << " " << i+1 << endl;
43+
foundanswer = true;
44+
break;
45+
}
46+
47+
}
48+
if (foundanswer == false) {
49+
cout << -1 << endl;
50+
}
51+
}
52+
return 0;
53+
}

0 commit comments

Comments
 (0)