Sliding Window Solution in C++ O(N) Solution


#1
    int Solution::solve(vector<int> &A, int k) {
        int i=0;
        int j=0;
        int n = A.size();
        int ans = -1;
        int count[2] {}; //count of 0 and 1 in the subsegment 
        while(j < n){
            count[A[j]]++;
            while(count[0] > k){
                count[A[i]]--;
                i++;
            }
            ans = max(ans, j-i+1);
            j++;
        }
        
        return ans;
    }