Data Structures & AlgorithmsVery hardCodeforces Div2 D · ICPC regional

How many contiguous subarrays of [3, 1, 3, 2, 5, 2, 5] contain exactly one occurrence of their maximum value?

  1. A. 24
  2. B. 21
  3. C. 19
  4. D. 28

Correct answer: B. 21

For each subarray, the maximum must appear exactly once. Counting with monotonic stacks on the nearest greater-or-equal elements on each side works in O(n) per element. Counting subarrays whose maximum appears at least once is a different (larger) quantity.

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

Exams that ask this topic