Data Structures & AlgorithmsHardGATE CS · ICPC regional

In the array [2, 5, 5, 3, 5, 2] the maximum of a subarray is taken. How many contiguous subarrays have their maximum value occurring exactly twice inside them?

  1. A. 6
  2. B. 7
  3. C. 8
  4. D. 10

Correct answer: A. 6

Count, for each subarray, its maximum and how often it occurs; a monotonic stack gives the ranges where a value is the maximum, and then the occurrences inside each range are counted.

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

Exams that ask this topic