Kth Largest Element in an Array

Given an integer array nums and an integer k, return the k^th largest element in the array.

Note that it is the k^th largest element in the sorted order, not the k^th distinct element.

Can you solve it without sorting?

Example 1
Inputnums = [3,2,1,5,6,4], k = 2
Output5
The 2nd largest element in sorted order is 5.
Example 2
Inputnums = [3,2,3,1,2,4,5,5,6], k = 4
Output4
The 4th largest element in sorted order is 4.

Constraints

  • 1 <= k <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4

Asked at 42 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate