[InterviewBit] Largest Continuous Sequence Zero Sum

Largest Continuous Sequence Zero Sum

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
vector<int> Solution::lszero(vector<int> &A) {
unordered_map<int,int> mp;
mp[0] = -1;
int start = -1, len = -1;
for(int i = 0, sum = 0; i < A.size(); i++) {
sum += A[i];
if(!mp.count(sum)) mp[sum] = i;
else {
int l = i - mp[sum];
if(len < l) {
start = mp[sum];
len = l;
}
}
}
vector<int> res;
for(int i = start + 1; i <= start + len; i++) res.push_back(A[i]);
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/11/PS/interviewbit/largest-continuous-sequence-zero-sum/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.