-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay26.cpp
37 lines (36 loc) · 848 Bytes
/
Day26.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
/*
Author: Aryan Yadav
Contiguous Array
Algorithm: Two pointer
Difficulty: Medium
*/
class Solution {
public:
int findMaxLength(vector<int>& nums) {
unordered_map<int, int> m;
int sum = 0;
int max_len = 0;
int end_ind = -1;
int n = nums.size();
for(int i=0;i<n;i++){
if(nums[i] == 0)
nums[i] = -1;
}
for(int i=0;i<n;i++){
sum+=nums[i];
if(sum == 0){
max_len = i+1;
end_ind = i;
}
if(m.find(sum+n) != m.end()){
if(max_len<i - m[sum+n]){
max_len = i - m[sum+n];
end_ind = i;
}
}
else
m[sum+n] = i;
}
return max_len;
}
};