Data Structures & AlgorithmsHardGATE CS · DSA

In the binary array [0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1] you may flip at most 2 zeros to ones. What is the length of the longest run of consecutive ones achievable? (Sliding window with at most 2 zeros inside.)

  1. A. 4
  2. B. 5
  3. C. 6
  4. D. 2

Correct answer: B. 5

Keep a window containing at most 2 zeros; when a third zero enters, move the left edge past the earliest zero. The widest such window has length 5. Simply adding 2 to the longest existing run (2) ignores that flips can join runs.

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

Exams that ask this topic