Data Structures & AlgorithmsHard

People stand in a row with heights [10, 6, 8, 5, 11, 9]. Person i can see person j > i if every person strictly between them is shorter than min(height[i], height[j]). How many (i, j) pairs of people can see each other in total?

  1. A. 8
  2. B. 5
  3. C. 15
  4. D. 10

Correct answer: A. 8

Visible pairs: (10,6), (10,8), (10,11), (6,8), (8,5), (8,11), (5,11), (11,9) = 8, since everyone between must be shorter than the shorter of the two. Adjacent pairs alone give 5, all pairs 15, and blocking only by the taller one (max instead of min) gives 10.

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

Exams that ask this topic