Data Structures & AlgorithmsVery hard

What is the maximum number of pairwise DISJOINT contiguous blocks of 1 -1 2 3 -3 4 -4 5 whose sums are all zero? (Blocks must not overlap; uncovered elements are allowed.)

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

Correct answer: D. 3

Interval DP over prefixes: the best count ending at position t is either the best ending at t−1 or a zero-sum block ending at t plus the best before its start. Counting all zero-sum blocks overlaps them, and a greedy scan can miss a better packing.

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

Exams that ask this topic