-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLongest_subset_zero_sum.cpp
More file actions
45 lines (43 loc) · 1.01 KB
/
Longest_subset_zero_sum.cpp
File metadata and controls
45 lines (43 loc) · 1.01 KB
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
#include <bits/stdc++.h>
using namespace std;
int longestSubsetZero(int *arr,int n)
{
int res = 0;
int sum=0;
unordered_map<int, int> m;
for (int i = 0; i < n; i++)
{
sum+=arr[i];
if(arr[i]==0 && n ==0)
res =1;
if(sum==0 )
{
int temp=i+1;
if(res<temp)
res=temp;
continue;
}
if(m.count(sum)>0 )
{
int temp=i-m[sum];
if(res<temp)
res=temp;
continue;
}
m[sum]=i;
}
unordered_map<int, int>::iterator it = m.begin();
while (it != m.end())
{
cout << "Key : " << it->first << " value : " << it->second << endl;
it++;
}
return res;
}
int main()
{
int arr[] = { 95, -97, -387, -435, -5, -70, 897, 127, 23, 284};
int arr1[] = { 6,1,5,-8,-4,2};
int n = 6;
cout << "Longest Subset with Sum Zero = " << longestSubsetZero(arr1, n);
}