Data Structures & AlgorithmsModerate

How many contiguous subarrays of [2, 4, 1, 3, 5, 2] have a sum in the closed range [5, 9]?

  1. A. 6
  2. B. 8
  3. C. 7
  4. D. 14

Correct answer: B. 8

Closed range count equals at-most(R) minus at-most(L − 1), which is the same as at-most(R) minus strictly-less-than(L). Subtracting at-most(L) removes the subarrays whose sum equals L, which must stay.

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

Exams that ask this topic