Kth Largest Element in an Array
Problem
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.
Solution Approach
Expected Time complexity:
Click - to see solution code
- C++
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
int n = nums.size();
nth_element(nums.begin(), nums.begin() + n - k, nums.end());
return nums[n - k];
}
};