Data Structures & AlgorithmsHardGATE CS · TCS NQT

What is the maximum number of pairwise disjoint non-empty contiguous subarrays of [1, -1, 2, -2, 3, -3, 4] that each have sum zero?

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

Correct answer: B. 3

Greedy works here: cut as soon as a prefix sum repeats, since ending the current zero-sum block earlier leaves more room. The answer is the number of such cuts.

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

Exams that ask this topic