Data Structures & AlgorithmsVery hardGATE CS · TCS NQT

In the binary array [1, 1, 0, 1, 1, 1, 0, 1, 1], you may flip exactly one 0 to 1 (or flip none). What is the length of the longest run of consecutive 1s after the best choice?

  1. A. 7
  2. B. 6
  3. C. 3
  4. D. 9

Correct answer: B. 6

A window with at most one zero: flipping index 6 joins 1 1 1 and 1 1 into 1 1 1 1 1 1 (indices 3–8) = 6. Counting all the ones (7) ignores adjacency, 3 is the best run without a flip, and 9 would need both zeros flipped.

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

Exams that ask this topic