DIY: Kth Largest Element in an Array

Solve the interview question "Kth Largest Element in an Array" in this lesson.

Problem statement

Find the kth largest element in an unsorted array.

Note: We need to find the kth largest element in the sorted order, not the kth distinct element.

Input

The input will be an unsorted array arr and an integer k. The following is an example input:

[3,2,1,5,6,4], 2

Output

The output will be an integer value. The following is an example output:

5

Assuming the example is sorted, 5 is the 2nd largest number in the above array

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.