File tree 3 files changed +62
-0
lines changed
3 files changed +62
-0
lines changed Original file line number Diff line number Diff line change
1
+ # subarraysum
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments