Data Structures & AlgorithmsHard

How many contiguous subarrays of 3 1 2 4 2 3 1 2 have a sum EXACTLY equal to twice their length?

  1. A. 9
  2. B. 2
  3. C. 32
  4. D. 7

Correct answer: A. 9

Subtract 2 from every element and count zero-sum subarrays, which is a prefix-sum count. Equating the sum to the length, or using ≥ instead of =, answers a different question.

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

Exams that ask this topic