C++ | Maximum Ones After Modification | O(n) using sliding window approach


#1
int Solution::solve(vector<int> &A, int B) {
    int i=0, j=0, maxLen = 0; 
    while(j<A.size()) {
        if(B>=0 and not A[j ++])  B --;
        if(B<0  and not A[i ++])  B ++;
        maxLen = max(maxLen, j-i);
    }
    return maxLen;
}