Data Structures & AlgorithmsModerate

How many contiguous subarrays of 5 2 7 3 9 4 1 of length at least 2 have pairwise DISTINCT values, and whose FIRST element is their maximum?

  1. A. 12
  2. B. 6
  3. C. 5
  4. D. 4

Correct answer: C. 5

Both the distinctness test and the first-maximum test must hold; with distinct values the maximum is unique, so ties do not arise. Dropping either condition, or moving the test to the last element, changes the count.

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

Exams that ask this topic