forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZeroSum.cpp
68 lines (59 loc) · 1.32 KB
/
ZeroSum.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
62
63
64
65
66
67
68
/**
Given an array of integers,
find the largest continuous subarray
with sum zero
**/
#include <bits/stdc++.h>
using namespace std;
vector<int> lszero(vector<int> &A) {
int start = -1, end = -1, m = 0, l = 0;
unordered_map<int, int> umap;
int sum = 0, n = A.size();
for(int i=0; i<n; i++) {
sum += A[i];
if(sum == 0) {
start = 0;
end = i;
m = i+1;
} else {
if(umap.find(sum) != umap.end()) {
l = i-umap[sum];
if(l > m) {
m = l;
start = umap[sum]+1;
end = i;
}
} else
umap[sum] = i;
}
}
if(start == -1)
return vector<int> (0);
vector<int> res(m);
for(int i=start; i<=end; i++)
res[i-start] = A[i];
return res;
}
int main() {
int n;
cin >> n;
vector<int> arr(n);
for(int i = 0; i < n; i++)
cin >> arr[i];
vector<int> ans = lszero(arr);
cout << "The longest subarray with sum zer is : \n";
for(int i = 0; i < ans.size(); i++)
cout << ans[i] << ' ';
cout << '\n';
return 0;
}
/**
Input :
5
1 2 -2 4 -4
Output:
The longest subarray with sum zer is :
2 -2 4 -4
Time Complexity : O(n)
Space Complexity : O(n)
**/