-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlargest-continuous-sequence-zero-sum-interviewbit.cpp
49 lines (42 loc) · 1.28 KB
/
largest-continuous-sequence-zero-sum-interviewbit.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
vector<int> Solution::lszero(vector<int> &A) {
int len = A.size();
int i, j, len_seq, sum = 0, max_len =0, end = -1, start = -1, len_present = 0;
unordered_map <int, int> len_map;
for (j = 0 ; j < len ; j++)
{
sum = sum + A[j];
if(sum == 0)
{
len_present = j + 1;
if(max_len < len_present)
{
max_len = len_present;
start = -1;
end = j;
}
}
else if(len_map.find(sum) != len_map.end())
{
len_present = j - len_map[sum] ;
//printf("-%d-", len_present);
if(max_len < len_present)
{
//printf("s %d ", sum);
max_len = len_present;
start = len_map[sum];
end = j;
//printf("_%d_%d_", start, end);
}
}
else
{
len_map[sum] = j;
}
}
vector<int> max_string;
for (i = start + 1 ; i <= end ; i++)
{
max_string.push_back(A[i]);
}
return max_string;
}