Data Structures & AlgorithmsModerate

How many contiguous subarrays of 2 5 1 3 5 2 4 have a sum EXACTLY equal to 5, the maximum value of the whole array?

  1. A. 2
  2. B. 8
  3. C. 20
  4. D. 3

Correct answer: A. 2

Sum equality against a fixed target is checked on every block, and the singleton 5s are only part of the answer. Using at-most or greater-than, or counting only single elements, changes the total.

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

Exams that ask this topic