Data Structures & AlgorithmsHardGATE CS · TCS NQT

How many contiguous subarrays of [2, -2, 3, 1, -4, 2, -1] have sum 0?

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

Correct answer: B. 4

Subarray sums are zero exactly when two prefix sums are equal, so count repeated prefix values with the empty prefix included.

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

Exams that ask this topic