Simple C++ O(nlogn) solution


#1
 int Solution::solve(vector<int> &A) {
    map < int , int > m;
    int o=0,z=0,ans = 0;
    m[0] = 0;
    for(int i=0;i<A.size();i++){
        if(A[i])
        o++;
        else
        z++;
        if(o-z==1)
        ans = max(ans,i+1);
        if(m.find(o-z)==m.end())
        m[o-z] = i;
        if(m.find(o-z-1)!=m.end())
        ans = max(ans,i - m[o-z-1]);
    }
    return ans;
}