DIY: Max Consecutive Ones III
Solve the interview question "Max Consecutive Ones III" in this lesson.
We'll cover the following
Problem statement
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.
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 1
s is 10
for the input example given above.
Coding exercise
Implement the longestOnes(nums, k)
function, wherein nums
is the binary array containing 0
s and 1
s and k
is the number of flips allowed. The function will return an integer value, which will represent the maximum number of consecutive 1
s.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.