Max Consecutive Ones III Problem
Max Consecutive Ones III Problem — ExecCode Easy DSA Practice
Solve the Max Consecutive Ones III problem on ExecCode. Free online easy DSA practice in Sliding Window. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Given a binary array nums and an integer k, find the longest contiguous stretch that can become all 1s after flipping at most k zeros. Treat the current window [left..right] as the stretch being tested. Expand right to include a new bit, count zeros inside the window, and shrink left only when the window needs more than k flips. Every time zeros <= k, the whole window is valid and can update the answer.
Examples
Input {"nums": [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0], "k": 2}; Output 6. Input {"nums": [0, 0, 1, 1, 0, 1], "k": 1}; Output 4. Input {"nums": [1, 1, 1, 1], "k": 0}; Output 4
Constraints
1 ≤ nums.length ≤ 10⁵ nums[i] is 0 or 1 0 ≤ k ≤ nums.length
Practice Max Consecutive Ones III free on ExecCode. Browse DSA problems, topic map, and placement guides.