Data Structures & AlgorithmsHard

In the binary array [1, 1, 0, 1, 1, 1, 0, 1], you must delete exactly one element. What is the length of the longest run of consecutive 1s that can remain afterwards?

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

Correct answer: D. 5

Deleting a 0 joins the two neighbouring runs of 1s, so the best deletion is the 0 that separates the longest pair of runs. Measuring the original longest run ignores the merge, the number of 1s is a total, and forcing a +1 is a common overcount.

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

Exams that ask this topic