Menu Close

Return the kth largest element in the array…FTC

Given an integer array nums and an integer k, return the kth largest element in the array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Input:
Array : [3,2,1,5,6,4]
Kth   : 2

Output: 5
def Kth(arr,k):
    L=sorted(arr,reverse=True)
    return L[k-1]

A=list(map(int,input("Nums : ").split(' '))) #or A=[3,2,1,5,6,4]
K=int(input("Kth : "))
print(Kth(A,K))

Input_1:
Nums : 3 2 1 5 6 4
Kth : 2

Output:
5



Input_2:
Nums : 3 2 3 1 2 4 5 5 6
Kth : 4

Output:
4


Illustration of the Output:

Executed using python3 linux terminal