-
Notifications
You must be signed in to change notification settings - Fork 0
/
0sumsubA.cpp
61 lines (52 loc) · 1.27 KB
/
0sumsubA.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
#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define f(i,a,b) for(i=a;i<b;i++)
#define fr(i,a,b) for(i=a;i>=b;i--)
#define sp "\t"
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define mod 1000000007
using namespace std;
int i,n,*a;
int maxzlen()
{
unordered_map<int, int> x;
int s=0,l=0;
f(i,0,n)
{
// Add current element to sum
s += a[i];
if (a[i] == 0 && l == 0)
l = 1;
if (s == 0)
l = i + 1;
// Look for this sum in Hash table
if (x.find(s) != x.end()) {
// If this sum is seen before, then update max_len
l = max(l, i - x[s]);
}
else {
// Else insert this sum with index in hash table
x[s] = i;
}
}
return l;
}
main()
{
cout<<"\nEnter the No. of Elements:";
cin>>n;
a=new (nothrow) int[n];
cout<<"\nEnter your Array:\n";
f(i,0,n)
cin>>*(a+i);
maxzlen();
cout<<"\nYour Array:\n";
f(i,0,n)
cout<<*(a+i)<<sp;
delete[] a;
cout<<"\nThe Max Length is :"<<maxzlen();
}