medium

Kth Largest Element in an Array

Given an unsorted integer array and an integer k, return the k-th largest element in sorted order (not the k-th distinct element). Aim for better than sorting the whole array.

Constraints
  • 1 ≤ k ≤ n ≤ 10^5
  • -10^4 ≤ nums[i] ≤ 10^4
Examples
in: nums = [3,2,1,5,6,4], k = 2
out: 5
in: nums = [3,2,3,1,2,4,5,5,6], k = 4
out: 4

Code it yourself

Solve in
Test execution is not yet available for this exercise.Practice journal →Draft saved in this browser.
Hints:
Which approach applies?

Choose an approach to check your pattern recognition, or reveal the discussion when you need help.