Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS

For the binary array [1, 0, 1, 1, 0, 1, 1, 1, 0, 1], you may flip at most 2 zeros to ones. What is the length of the longest run of ones you can obtain?

  1. A. 7
  2. B. 3
  3. C. 5
  4. D. 8

Correct answer: D. 8

A window with at most two zeros: indices 2–9 (1 1 0 1 1 1 0 1) has length 8. Counting all the ones gives 7, the longest run with no flips is 3, and adding k = 2 to that run gives 5, missing that flips join runs.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic