Data Structures & AlgorithmsVery hard

Why is prefix-sum counting of zero-sum subarrays unaffected by the choice of starting index for the prefix array?

  1. A. Prefix sums always start at zero by definition, so the count changes with the start.
  2. B. A subarray sum is the difference of two prefix sums, so the starting constant cancels out in every comparison.
  3. C. The difference of two prefix sums is always zero.
  4. D. Only the last prefix sum is used by the algorithm.

Correct answer: B. A subarray sum is the difference of two prefix sums, so the starting constant cancels out in every comparison.

Adding a constant to every prefix sum leaves all differences unchanged, so equal-prefix pairs are the same. This is why an empty prefix of zero is the natural seed.

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

Exams that ask this topic