Data Structures & AlgorithmsVery hardFlipkart/Uber OA · placement

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

  1. A. 17
  2. B. 7
  3. C. 10
  4. D. 9

Correct answer: D. 9

atMost(10) = 17 and atMost(5) = 8, so the count is 9. Subtracting atMost(6) instead of atMost(5) wrongly drops subarrays whose sum is exactly 6.

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

Exams that ask this topic