O21

Max Consecutive Ones III

Mid / Senior Engineer
Target Time: 25 - 35 Minutes
Type: NUMERIC

Problem Statement & Objective

Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's to 1's.

Function Signature (Python)

longest_ones_after_flips(nums: list[int], k: int) -> int

Input & Output Contract

Output Rule:Maximum length of contiguous 1s with at most k flips.

System Constraints

1 <= nums.length <= 10^5
nums[i] is either 0 or 1
0 <= k <= nums.length

Example Execution Walkthrough

nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
result: 6

Example Test Scenarios (3)

Example 1
Input:[[1,1,1,0,0,0,1,1,1,1,0],2]
Expected Output:6
Example 2
Input:[[0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1],3]
Expected Output:10
Example 3: Tek Çevirme
Input:[[0,0,0],1]
Expected Output:1
Loading Python Editor...
Input Parameters
[
  [
    1,
    1,
    1,
    0,
    0,
    0,
    1,
    1,
    1,
    1,
    0
  ],
  2
]
Expected Output
6