Longest Subarray with Equal 0s and 1s After K Flips

EXTREME · C · 5000ms

Finally, Tanvir is auditing a binary eligibility list (1 = eligible, 0 = not) and is allowed to "appeal" at most K non-eligible marks into eligible ones — he needs the longest stretch of eligible entries achievable. Read a binary array (0s and 1s) of size N and an integer K. You may flip at most K zeros to ones. Print the length of the longest contiguous subarray consisting only of 1s achievable after the flips. Use a sliding window, O(N).

Input

Line1: N K. Line2: N binary values.

Output

Length of the longest all-1s subarray achievable.

Constraints

1 ≤ N ≤ 10^5, 0 ≤ K ≤ N

Sample Input

11 2
1 1 1 0 0 0 1 1 1 1 0

Sample Output

5
main.c
Loading editor…

Write C, then Run (custom I/O) or Submit (sample tests). ⌘/Ctrl+Enter submits.

Longest Subarray with Equal 0s and 1s After K Flips · DIU ContestHub