K-th Largest Element
EasyPractice library · +10 XPheapsk-th element
Given a list of N integers and an integer K, find the K-th largest element in the list. For example, if the list is [3, 2, 1, 5, 6, 4] and K is 2, the 2nd largest element is 5. If the list is [3, 2, 3, 1, 2, 4, 5, 5, 6] and K is 4, the 4th largest element is 4.
Input
The first line contains two integers N and K, separated by a space. The second line contains N integers, representing the list.
Output
Print a single integer, the K-th largest element in the list.
Constraints
1 <= N <= 1000 1 <= K <= N -10^9 <= element <= 10^9
Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests
Example 1
Input
6 2 3 2 1 5 6 4
Output
5
The 2nd largest element in [3, 2, 1, 5, 6, 4] is 5.
Example 2
Input
9 4 3 2 3 1 2 4 5 5 6
Output
4
The 4th largest element in [3, 2, 3, 1, 2, 4, 5, 5, 6] is 4.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
6 2 3 2 1 5 6 4
Example 2 input
9 4 3 2 3 1 2 4 5 5 6