Design Gurus Logo
Max Consecutive Ones III (medium)

Problem Statement

Given an array nums containing 1 and 0 digits, return the maximum number of consecutive 1s if you can flip at most any k 0s.

Examples

  • Example 1:

    • Input: nums = [1, 0, 1, 1, 0, 0, 1, 1, 1, 0], k = 2
    • Expected Output: 7
    • Justification: Flipping the two 0s in the middle section [0, 0] results in the longest continuous sequence of 1s: [1, 1, 1, 1, 1, 1, 1].
  • Example 2:

    • Input: nums = [0, 1, 1, 0, 1, 0, 1, 0, 1], k = 1
    • Expected Output: 4
    • Justification: Flipping the 0 at index 3 will yield a maximum continuous sequence of four 1s.
  • Example 3:

    • Input: nums = [1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1], k = 3
    • Expected Output: 8
    • Justification: Flipping the three 0s in the middle [0, 0, 0] results in the longest sequence of 1s: [1, 1, 1, 1, 1, 1, 1, 1].

Constraints:

  • 1 <= nums.length <= 10<sup>5</sup>
  • nums[i] is either 0 or 1.
  • 0 <= k <= nums.length

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory