Data Structures & AlgorithmsModerateTCS NQT · GATE CS

How many DISTINCT values appear among the sums of all non-empty contiguous subarrays of [2, -1, 3, 0, -2, 4]?

  1. A. 21
  2. B. 10
  3. C. 7
  4. D. 9

Correct answer: D. 9

Count the set of subarray sums rather than subarrays: several subarrays can have the same sum (for example the zero-sum ones), so the distinct count is smaller than the number of subarrays.

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

Exams that ask this topic