Data Structures & AlgorithmsModerateICPC regional · Amazon SDE OA

How many pairs i < j in [4, 1, 5, 2, 3] satisfy a[i] > a[j] and every element strictly between them is at most a[i]?

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

Correct answer: C. 3

Each qualifying pair is visible from a[i] over the elements in between, which is a monotonic-stack idea. Dropping the between-condition (the last option) counts all inversions instead.

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

Exams that ask this topic