-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontiguous_array.txt
More file actions
27 lines (26 loc) · 839 Bytes
/
Copy pathcontiguous_array.txt
File metadata and controls
27 lines (26 loc) · 839 Bytes
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
class Solution {
public:
int findMaxLength(vector<int>& nums) {
unordered_map<int,int> mp; //map of <gain,index> form
mp[0]=-1; //add starting index with default gain of 0 at -1
//change all zeros to -1
for(int i=0;i<nums.size();i++){
nums[i]==0?nums[i]=-1:nums[i]=1;
}
int sum=0,res=0;
for(int i=0;i<nums.size();i++){
//cumulative sum
sum+=nums[i];
//check if value already exists in the map
if(mp.find(sum)!=mp.end()){
//finding the length of possible subarray and comparing with the max result
res=max(res,i-mp[sum]);
}
else{
//adding value to our map
mp[sum]=i;
}
}
return res;
}
};