Data Structures & AlgorithmsModerateCodeforces Div2 C · TCS NQT

In the binary array [1, 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 1s you can make?

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

Correct answer: B. 5

Sliding window with at most k zeros inside: once the window holds more than k zeros, shrink from the left. The window length after the flips is the answer. Counting only maximal runs of ones misses the bridges that flips create.

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

Exams that ask this topic