Skip to content

Commit 2bb1457

Browse files
Add files via upload
1 parent 98b12c9 commit 2bb1457

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Hashing/Contiguous Array.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
int Solution::solve(vector<int> &A) {
2+
int n=A.size();
3+
if(n==0)
4+
return 0;
5+
unordered_map<int,int> mp;
6+
int sum=0,ans=0;
7+
mp[0]=-1;
8+
for(int i=0; i<A.size(); ++i){
9+
if(A[i]==0)
10+
--sum;
11+
else
12+
++sum;
13+
if(mp.find(sum)==mp.end())
14+
mp[sum]=i;
15+
else
16+
ans=max(ans,i-mp[sum]);
17+
}
18+
return ans;
19+
20+
}

0 commit comments

Comments
 (0)