Data Structures & AlgorithmsVery hard

When counting subarrays with sum in a range [L, R] for an array with zeros, why can a single subarray start be counted many times with a naive window?

  1. A. Zeros let the same sum be reached by different ends, so each (start, end) pair must be counted separately rather than merged.
  2. B. Zeros are removed automatically.
  3. C. Subarrays containing zero are always equal to zero.
  4. D. Only the first zero matters.

Correct answer: A. Zeros let the same sum be reached by different ends, so each (start, end) pair must be counted separately rather than merged.

Each start and end pair is a different subarray even when sums coincide, so the count must be over index pairs. Prefix-sum counting handles this by counting pairs of prefix indices.

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

Exams that ask this topic