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?
- A. 9
- B. 7
- C. 6
- 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.
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
- How many contiguous subarrays of [3, 1, 4, 1, 5, 9, 2] have their FIRST element equal to their minimum (ties allowed)?
- A linked list holds the values [1, 2, 3, 4, 5, 6, 7, 8]. Reverse every complete group of 3 consecutive nodes; a final group with fewer…
- You may delete one contiguous block from [4, -9, 3, 5, -2, 7, -8, 6] (possibly empty block) and keep the prefix before it and the suffix…
- What is the length of the longest common prefix of the two strings obtained by taking all cyclic rotations of "banana" and keeping only…
- Count the NON-EMPTY subsets of the 12 items 3 5 6 7 2 9 4 8 1 10 11 12 (chosen by position) whose sum is divisible by 4.
- How many NON-EMPTY subsequences of 1 2 3 5 6 8 9 (subsets chosen by position) have (maximum − minimum) ≤ 2?