Skip to content
BASA DSA

Kth Largest Element In An Array

MediumHeap / Priority Queue

Return the kth largest value in nums, counting repeated values separately.

Example 1

Input
nums = [4,1,7,7,2]
k = 2
Output
7

Explanation

Descending order is 7,7,4,2,1; duplicates count, making the second value 7.

Example 2

Input
nums = [3,2,1]
k = 3
Output
1

Explanation

The third largest among three values is minimum 1.

Example 3

Input
nums = [-4]
k = 1
Output
-4

Explanation

The sole negative value is also largest.

Constraints

  • 1 <= k <= len(nums) <= 100000
  • Values are -10000..10000

Function contractfindKthLargestArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…