Data Structures & AlgorithmsVery hardCodeforces Div2 D · ICPC regional

People stand in a row with heights [3, 1, 4, 2, 5, 2]. Two people can see each other if every person strictly between them is shorter than BOTH of them. How many pairs of people can see each other?

  1. A. 9
  2. B. 7
  3. C. 6
  4. D. 11

Correct answer: B. 7

Use a monotonic stack on the way left to right: each person pops shorter people (those pairs are visible) and equal-height handling needs care. The brute check 'all between are shorter than both' is the definition to match.

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

Exams that ask this topic