Data Structures & AlgorithmsVery hard

How many contiguous subarrays of 3 1 4 1 5 9 2 6 contain the position of the global maximum value 9?

  1. A. 14
  2. B. 17
  3. C. 18
  4. D. 19

Correct answer: C. 18

A subarray contains the position of the maximum when its left end is at or before it and its right end is at or after it, giving (pos+1)·(n−pos) blocks. Subtracting one removes a block that actually qualifies; using the minimum or excluding singletons gives other totals.

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

Exams that ask this topic