Data Structures & AlgorithmsHardGATE CS · TCS NQT

How many contiguous subarrays of [1, 2, -3, 3, -3, 2, 1] have sum exactly zero?

  1. A. 6
  2. B. 7
  3. C. 8
  4. D. 10

Correct answer: A. 6

A zero-sum subarray is a pair of equal prefix sums, so count repeats among the prefix sums (with the empty prefix 0) using a hash map.

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

Exams that ask this topic