PIXELBANKv9.1.0
Menu

Kth Largest Element in an Array

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
solution.py

Test Results

0/0
Run code to see test results.
Kth Largest Element in an Array - Medium | PixelBank