Data Structures & AlgorithmsHardCodeforces Div2 D · TCS NQT

How many contiguous subarrays of [2, 5, 1, 5, 3, 4, 5] have a maximum element exactly equal to 5?

  1. A. 23
  2. B. 26
  3. C. 21
  4. D. 3

Correct answer: A. 23

Count subarrays with maximum ≤ 5 minus subarrays with maximum ≤ 4, or use the nearest-greater boundaries for each 5. Counting only the positions equal to 5 ignores subarrays that contain a 5 as their maximum with other values.

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

Exams that ask this topic