Untitled

 avatar
unknown
plain_text
2 years ago
557 B
6
Indexable
class Solution {
public:
    int findMaxLength(vector<int>& nums) {
        unordered_map<int,int>map;
        map[0]=-1; //equalk no of zeros and 1s becomes the length from starting ..
        int n=nums.size();
        int sum=0;
        int res=0;
        for(int i=0;i<n;i++){
            if(nums[i]==1) sum+=1;
            else sum+=-1;
            
            if(map.find(sum)!=map.end()){
                res=max(res,i-map[sum]);
            }else{
                map[sum]=i;
            }
        }
        return res;
    }
};
Editor is loading...
Leave a Comment