Same as find the largest sub-array with sum 1( after some modification )


#1
int Solution::solve(vector<int> &A) 
{
    unordered_map<int,int>m;
    int sum=0,max_len=INT_MIN;
    for(int i=0;i<A.size();i++)
    {
        A[i]=(A[i]==0?-1:1); //We will change the zeros to -1 for ease
        // Now this problem has reduced to find the largest subarray with sum 1
        sum+=A[i];
        if(sum==1)
        {
            max_len=max(max_len,i+1);
        }
        if(m.find(sum)==m.end())
        {
            m[sum]=i;
        }
        if(m.find(sum-1)!=m.end())
        {
            max_len=max(max_len,i-m[sum-1]);
        }
    }
    return max_len;
}