Data Structures & AlgorithmsHardCodeforces Div2 D · TCS NQT

What is the maximum number of DISJOINT non-empty contiguous subarrays of [1, -1, 2, -2, 3, 4, -7, 5, -5, 6] that each have sum 0?

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

Correct answer: A. 4

dp[j] = max(dp[j−1], dp[i] + 1) over starts i with zero-sum a[i..j−1]: a greedy scan that resets the prefix set at each zero-sum match is the fast form. Both give the same count here.

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

Exams that ask this topic