Data Structures & AlgorithmsVery hardCodeforces Div2 B-D · ICPC regional

For the positive array [2, 1, 3, 4, 1], how many contiguous subarrays have a sum at most 5?

  1. A. 8
  2. B. 7
  3. C. 15
  4. D. 5

Correct answer: A. 8

With positive elements the valid left ends for each right end form a range, so adding window lengths gives 1 + 2 + 2 + 1 + 2 = 8. Using < 5 instead of ≤ 5 gives 7, 15 counts every subarray and 5 only the single elements.

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

Exams that ask this topic