Solution to Max Consecutive Ones III problem. See this Youtube video for the explanation.

class Solution {
    public int longestOnes(int[] nums, int k) {
        int n = nums.length;
        if (n <= k) return n;
        
        // Sliding window pointers
        int L = 0, R = 0;

        // Track flip count and max length
        int flipCount = 0, maxLen = 0;

        // Slide over the array elements
        while(R < n) {
            if (nums[R] == 0) {
                flipCount++;
            }

            // contract window if we don't meet the condition
            while(flipCount > k) {
                if (nums[L] == 0) {
                    flipCount--;
                }
                L++;
            }

            // update maxlen
            maxLen = Math.max(maxLen, R - L + 1);

            R++;
        }

        return maxLen;

    }
}