#C5998. Find the Kth Tallest Plant
Find the Kth Tallest Plant
Find the Kth Tallest Plant
You are given an array of plant heights and multiple queries. For each query, you need to find the Kth tallest plant in a specified subarray, i.e. the range from L to R (both inclusive). If the query asks for a plant that does not exist (i.e. when K is larger than the number of plants in the range), output "Out of range".
The problem can be formulated as follows: Let \( A \) be the subarray of heights from index L to R. Sort \( A \) in descending order so that \( A[0] \) is the tallest. The answer for the query is then \( A[K-1] \) if \( K \) does not exceed the length of \( A \); otherwise, output "Out of range".
inputFormat
The first line of input contains two integers \( N \) and \( Q \), representing the number of plants and the number of queries respectively.
The second line contains \( N \) space-separated integers representing the heights of the plants.
The next \( Q \) lines each contain three space-separated integers \( L \), \( R \), and \( K \), specifying the starting index, ending index (1-indexed) of the range, and the order (K) of the tallest plant required.
outputFormat
For each query, output a single line containing the height of the Kth tallest plant in the specified range. If such a plant does not exist, output "Out of range".
## sample7 3
10 20 15 25 30 5 40
2 5 2
1 7 3
3 6 5
25
25
Out of range
</p>