Data Structures & AlgorithmsModerate

How many contiguous subarrays of positive integers [2, 1, 4, 3, 1, 2] have a sum at most 6?

  1. A. 11
  2. B. 10
  3. C. 5
  4. D. 12

Correct answer: A. 11

With positive values a two-pointer window shrinks while the sum is too large, and each right end adds (right − left + 1) valid starts. Adding the left index instead of a width, or using < instead of ≤, changes the count.

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

Exams that ask this topic