Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS

How many contiguous subarrays of [2, 5, 1, 4, 3] have their maximum equal to the first or the last element of the subarray?

  1. A. 12
  2. B. 10
  3. C. 15
  4. D. 11

Correct answer: D. 11

Brute force over all 15 subarrays settles it; the monotonic-stack solution counts, for each element as the maximum, the ends that may be chosen. Ties must be handled consistently, but no tie occurs here.

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

Exams that ask this topic