Kth Largest Element in an Array

Medium

Description

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.

Examples

Input:nums = [3,2,1,5,6,4], k = 2
Output:5
Explanation:

The 2nd largest element is 5.

Input:nums = [3,2,3,1,2,4,5,5,6], k = 4
Output:4
Explanation:

The 4th largest element is 4.

Input:nums = [7, 10, 4, 3, 20, 15], k = 3
Output:10
Explanation:

When sorted in descending order: [20, 15, 10, 7, 4, 3]. The 3rd largest element is 10.

Constraints

  • 1 ≤ k ≤ nums.length ≤ 10⁵
  • -10⁴ ≤ nums[i] ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!