-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndexes of Subarray Sum.cpp
61 lines (50 loc) · 1.18 KB
/
Indexes of Subarray Sum.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
54
55
56
57
58
59
60
61
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
vector<int> subarraySum(vector<int> &arr, int target) {
// code here
int n = arr.size();
int start = 0, current_sum = 0;
for (int end = 0; end < n; end++) {
current_sum += arr[end];
while (current_sum > target && start <= end) {
current_sum -= arr[start];
start++;
}
if (current_sum == target) {
return {start + 1, end + 1};
}
}
return {-1};
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
cin.ignore();
while (t--) {
vector<int> arr;
int d;
string input;
getline(cin, input);
stringstream ss(input);
int number;
while (ss >> number) {
arr.push_back(number);
}
cin >> d;
cin.ignore();
Solution ob;
vector<int> result = ob.subarraySum(arr, d);
for (int i : result) {
cout << i << " ";
}
cout << "\n~\n";
}
return 0;
}
// } Driver Code Ends