Data Structures & AlgorithmsHard

For [2, 1, 3, 5, 4, 6, 8, 7, 9], how many indices i with 1 ≤ i ≤ n−2 satisfy: every element before i is strictly less than a[i], and every element after i is strictly greater than a[i]?

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

Correct answer: B. 2

The condition needs the prefix maximum below a[i] and the suffix minimum above a[i]. Checking only one side, or relaxing both to ≤, changes which indices qualify.

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

Exams that ask this topic