Problem 304
Max Consecutive Ones with K Flips
Medium · 20 pts · Array, Sliding Window, Two Pointers
Problem
You are given a binary array and an integer K. You may change at most K of the 0s into 1s. After making your changes, what is the length of the longest block of consecutive 1s you can get? You do not have to use all K flips. Think about it as finding the longest window of the array that contains at most K zeros.
Input
The first line contains N and K. The second line contains N values, each 0 or 1.
Output
Print the maximum possible length of a block of consecutive 1s.
Constraints
1 <= N <= 100000; 0 <= K <= N; A[i] is 0 or 1
Example input
11 2 1 1 1 0 0 0 1 1 1 1 0
Example output
6
Explanation
In 1 1 1 0 0 0 1 1 1 1 0 with K = 2, flip the zeros at positions 4 and 5 (counting from 0). Positions 4 to 9 then hold six consecutive 1s, and no window containing at most two zeros is longer, so the answer is 6.