Data Structures & AlgorithmsVery hard

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

  1. A. 21
  2. B. 7
  3. C. 11
  4. D. 3

Correct answer: C. 11

Count the set of sums, not the number of subarrays: many different subarrays share a sum here. Total subarrays counts with multiplicity, and the set of prefix sums (including 0) is a different collection from the set of subarray sums.

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

Exams that ask this topic