DIY: Max Consecutive Ones III

Solve the interview question "Max Consecutive Ones III" in this lesson.

Problem statement

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 0s.

Input

The input will be a binary array, nums, and an integer, k, which will represent the number of flips allowed, that is, 0 -> 1.

nums = [0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1]
k = 3

Output

The output will be an integer value. The following is an example output for the input given above:

10

The maximum number of consecutive 1s is 10 for the input example given above.

Coding exercise

Implement the longestOnes(nums, k) function, wherein nums is the binary array containing 0s and 1s and k is the number of flips allowed. The function will return an integer value, which will represent the maximum number of consecutive 1s.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.