A scoreboard stores n distinct integer scores. Starting from an empty binary search tree, insert the scores in the order given, using the standard BST rule: to insert a key x, walk down from the root, going left if x is smaller than the current node and right if x is larger, until an empty spot is reached, where a new node is created. No rebalancing is performed.
You are then given a single integer k. Report the k-th largest score in the tree (so k = 1 is the maximum score, k = n is the minimum score).
Line 1: an integer n.
Line 2: n space-separated distinct integers — the scores, in insertion order.
Line 3: an integer k.
A single integer: the k-th largest score.
Example 1
Input
5 5 3 8 1 4 2
Expected
5
Explanation
Scores sorted descending are 8,5,4,3,1. The 2nd largest is 5.
Example 2
Input
4 -3 -7 10 2 1
Expected
10
Explanation
Scores sorted descending are 10,2,-3,-7. The 1st largest (the maximum) is 10.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →