v9.1.1
Menu

Kth Largest Element in an Array

mediumFREESorting & SearchingNetflix DSA

Given an integer array nums and an integer k, return the kth largest element.

Note: it is the kth largest in sorted order, not the kth distinct element.

Example

Input
3,2,1,5,6,4
2
Output
5
Reasoning
  • First, we sort the input array in descending order: 6,5,4,3,2,16, 5, 4, 3, 2, 1
  • Then, we select the element at the kthk^{th} position, where k=2k = 2
  • The element at the 2nd2^{nd} position in the sorted array is 55
  • The final output is 55

Constraints

  • 1 <= k <= len(nums) <= 10^5
  • -10^4 <= nums[i] <= 10^4

Learn this in

More from Netflix DSA

solution.py

Test results

Sign in to run your code against the test cases.