-
Notifications
You must be signed in to change notification settings - Fork 0
/
#102.cpp
48 lines (37 loc) · 1001 Bytes
/
#102.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
#include<iostream>
#include<unordered_map>
#include<vector>
using namespace std;
vector<int>* subarray_from_indices(vector<int>& v, int left, int right) {
vector<int>* ret = new vector<int>;
for (int i = left; i <= right; i++) {
ret->push_back(v[i]);
}
return ret;
}
vector<int>* contiguous_sum(vector<int>& v, int sum) {
unordered_map<int, int> array_sums;
int curr_sum = 0;
for (int i = 0; i < v.size(); i++) {
curr_sum += v[i];
if (curr_sum == sum) return subarray_from_indices(v, 0, i);
else if (array_sums.find(curr_sum - sum) != array_sums.end())
return subarray_from_indices(v, array_sums[curr_sum - sum] + 1, i);
else array_sums[curr_sum] = i;
}
return nullptr;
}
int main() {
vector<int> v = {-10,0,50,-30,-20,10};
int k = 20;
vector<int>* result = contiguous_sum(v, k);
if (result == nullptr)
cout << "No sub-array found." << endl;
else {
cout << "sum([";
for (int i : *result) {
cout << i << ",";
}
cout << "]) == " << k << endl;
}
}