Skip to content

Commit

Permalink
Largest Subarray with Zero Sum
Browse files Browse the repository at this point in the history
  • Loading branch information
Deep0409 committed Oct 6, 2022
1 parent d5db3bd commit 15adf2e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions Solutions/subarraywith0sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <bits/stdc++.h>
using namespace std;


class Solution{
public:
int maxLen(vector<int>&A, int n)
{
unordered_map<int,int>psum;
// Your code here
int len=0, temp=0;;
for(int i=0;i<A.size();i++)
{
temp+=A[i];
if(A[i]==0&&len==0)
len=1;
if(temp==0)
len=i+1;
if(psum.find(temp)!=psum.end())
len=max(len,i-psum[temp]);
else
psum[temp]=i;

}
return len;
}
};


int main()
{
int t;
cin>>t;
while(t--)
{
int m;
cin>>m;
vector<int>array1(m);

for(int i=0;i<m;i++)
{
cin>>array1[i];
}
Solution ob;
cout<<ob.maxLen(array1,m)<<endl;
}
return 0;
}
Binary file added Solutions/subarraywith0sum.exe
Binary file not shown.

0 comments on commit 15adf2e

Please sign in to comment.