Data Structures & AlgorithmsHardCodeforces Div2 D

How many non-empty contiguous subarrays of [4, -2, 7, -5, 3, 6] have a sum of at most 4?

  1. A. 8
  2. B. 13
  3. C. 21
  4. D. 11

Correct answer: D. 11

With negative values the sliding-window trick fails, because adding an element can lower the sum. Prefix sums with a sorted structure (or brute force on small n) count pairs P[j] − P[i] ≤ limit; reading the total number of subarrays is a different count.

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

Exams that ask this topic