Max Consecutive Ones III
Given a binary array nums and an integer k, return the maximum number of consecutive 1s in the array if you can flip at most k zeros to ones.
Example 1
Input
nums = [1,1,1,0,0,0,1,1,1,1,0] k = 2
Output
6
Explanation
We can flip at most two zeros inside a valid window. The longest window that can be converted into all 1s contains 6 elements.
Example 2
Input
nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1] k = 3
Output
10
Explanation
A window containing at most three zeros can be converted into consecutive 1s by flipping those zeros. The longest such window has length 10.
Constraints
Hints:
Hint 1
Maintain a sliding window containing at most k zeros.
Hint 2
Expand the window using the right pointer.
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 17, 2026
Last updated
Aug 17, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output