DIY: Kth Largest Element in an Array
Solve the interview question "Kth Largest Element in an Array" in this lesson.
We'll cover the following
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
Coding exercise
Implement the findKthLargest(arr, k)
function, where arr
is the array and k
is the integer value. The function returns the kth
largest value in the sorted arr
.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.