Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Max Consecutive Ones III

Medium
sliding-windowarray

Given a binary array nums and an integer k, return the length of the longest contiguous run of 1s achievable if you may flip at most k zeros.

Input

  • Line 1: space-separated 0/1 integers
  • Line 2: integer k

Output: the maximum length.

Example 1
Input:
1 1 1 0 0 0 1 1 1 1 0
2
Output:
6

Flip the last two 0s in the window [0,0,1,1,1,1].

Example 2
Input:
0 0 1 1 0 0 1 1 1 0 1 1 0 0 0 1 1 1 1
3
Output:
10

Constraints

  • 1 ≤ nums.length ≤ 10^5
  • nums[i] ∈ {0, 1}
  • 0 ≤ k ≤ nums.length
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.