Skip to content

ORIGINAL CODING QUESTION

Longest streak with flip budget

Flip at most k zero flags into ones. Return the longest achievable contiguous one streak.

Open practice workspace

Original exercise · medium. Editorial role relevance does not imply a company has asked this question.

Input and output

n, then n binary flags, then k.

Constraints

1 ≤ n ≤ 200; flags in {0,1}; 0 ≤ k ≤ n.

Example 1

Input

6 1 0 1 0 1 1 1

Output

4

Flipping the second zero produces a length-four ending streak.

Example 2

Input

3 0 0 0 0

Output

0

No flips means no one streak.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Track how many zeros are in the current window.

Hint 2

A window is valid while its zero count is at most k.

Hint 3

Shrink from the left when the flip budget is exceeded.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →