Data Structures & AlgorithmsVery hard

How many contiguous subarrays of 1 4 2 6 3 5 have an AVERAGE that is at least 3?

  1. A. 15
  2. B. 17
  3. C. 21
  4. D. 4

Correct answer: B. 17

Average ≥ 3 ⇔ Σ(a[i] − 3) ≥ 0 over the subarray, a prefix-sum test: 17 subarrays pass. Using a strict > gives 15, 21 counts every subarray, and 4 counts only the single elements that are at least 3.

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

Exams that ask this topic