Skip to content

Commit 06b12a7

Browse files
authored
Create Contiguous Array.cpp
1 parent be8f9ec commit 06b12a7

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Contiguous Array.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int findMaxLength(vector<int>& nums) {
4+
int sum=0, maxLen=0;
5+
map<int, int> seen{{0, -1}};
6+
for(int i=0; i<nums.size(); i++){
7+
sum += nums[i]==1 ? 1 : -1;
8+
if(seen.count(sum)) maxLen = max(maxLen, i-seen[sum]);
9+
else seen[sum] = i;
10+
}
11+
return maxLen;
12+
}
13+
};

0 commit comments

Comments
 (0)