Given an unsorted array
arr
of size
n
and an integer
k
, write a program to find the
k’th largest element
in the given array.
Problem Note
- It is the kth largest element in the sorted order, not the kth distinct element.
- Constraints : 1 <= K <=n
- Try to do it using constant extra space.
Example 1
Input: arr[] = [6, 5, 1, 8, 9] and k = 2
Output: 8
Explanation: 8 is the 2nd largest element in the array.
Example 2
Input: arr[] = [1, 2, 3, 4, 5] and k = 2
Output: 4
Explanation: 4 is the 2nd largest element in the array.