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?
- A. 8
- B. 5
- C. 15
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- In the unsorted array 100 4 200 1 3 2 4 101 102 103 3, what is the length of the longest run of CONSECUTIVE integers (values like 5, 6, 7…
- For the array 1 2 3 4, how many DISTINCT values appear as the XOR of a non-empty contiguous subarray?
- Starting at index 0 of 2 3 1 1 4 2 3 1 1 4, a value a[i] lets you jump from index i to any index in i+1 … i+a[i]. What is the minimum…
- Remove duplicate values from the unsorted list 4 2 4 1 2 5 1 so that only ONE copy of each value remains, keeping the copy that appears…
- A single stack, starting empty, is used to sort the input permutation of 1..4 into 1, 2, 3, 4 at the output (each input element is pushed…
- Two sorted lists A = 10 30 50 and B = 5 40 60 80 90 are merged by taking nodes ALTERNATELY (first from A, then from B, and so on) until…