Max Consecutive Ones III

Practice this coding question on Coding Gurukul.

Problem statement

A reliability dashboard records success as 1 and failure as 0. An operator can correct at most k failed readings and wants the longest continuous success run that could then appear in the log. Your task Find the maximum length of a contiguous subarray containing only ones after changing at most k zeroes to ones. Important details Corrections may be unused, and existing ones do not consume the budget. The chosen run must remain contiguous in the original log. When k is zero, only existing runs of ones qualify. Worked example For [1, 0, 1, 0, 1] with k = 1, either interior zero can be changed. This creates a run of three ones; changing both would exceed the budget.

Input format

First line: n k. Next line: n binary values.

Output format

Print the longest possible run length.

Constraints

1 <= n <= 100000; 0 <= k <= n; values are 0 or 1.

Sample input

5 1
1 0 1 0 1

Sample output

3

Sign in to run or submit a solution in C++, Java or Python.