Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
557 B
2
Indexable
Never
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;
    }
};
Leave a Comment