Data Structures & AlgorithmsVery hardGATE CS · DSA

How many contiguous subarrays of [5, 4, 4, 3, 2, 5, 2, 2] have a sum between 7 and 11 inclusive? (All values positive: count atMost(11) − atMost(6) with sliding windows.)

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

Correct answer: D. 11

atMost(11) = 21 and atMost(6) = 10, so the count is 11. Subtracting atMost(7) instead of atMost(6) wrongly drops subarrays whose sum is exactly 7.

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

Exams that ask this topic