Data Structures & AlgorithmsHard

How many contiguous subarrays of 2 1 4 7 3 2 5 3 1 are mountains? A mountain has length at least 3, strictly increases to a single peak, and then strictly decreases.

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

Correct answer: C. 6

A mountain is a bitonic block of length at least 3 whose peak is interior; a block with two equal neighbours is not strictly rising. Dropping the length rule, allowing monotone blocks, or counting peaks instead of blocks each gives a different number.

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

Exams that ask this topic