Kth Largest Value (Set 13)

Medium

Given an array of integers nums and a k with 1 <= k <= nums.length, return the k-th largest value. Duplicates count separately, so k = 1 is the maximum.

Kth Largest Value (Set 13) — the data. This set runs 4 cases: nums holds arrays of 48–48 items, with values from -29 to 59; k ranges over 1–3. The hidden cases are the largest of the set.

Example 1

kthLargestValue()
Input
nums = [59, 13, -28, 53, 19, 25, 54, -14, -20, 26, -7, 51, 19, -11, 50, 27, 51, 51, 49, 47, 44, 43, 3, 57, 1, 47, -22, -24, 49, 47, 56, 2, -23, 37, -25, 24, -20, 58, -24, 11, -22, -4, 44, 31, 20, 51, 39, 9], k = 1
Output
59

Why

`kthLargestValue` returns `59` for this input.

Example 2

kthLargestValue()
Input
nums = [36, -8, 57, 38, 40, -24, 33, 39, 55, 16, 20, 9, 58, -16, -2, 45, 5, -10, -26, 22, 11, 34, 22, 20, 24, 0, 32, -1, 12, 1, -6, 30, 39, 58, 22, 33, 30, 51, 19, -9, -12, 57, 12, 4, 30, 11, 48, 24], k = 2
Output
58

Why

`kthLargestValue` returns `58` for this input.
Constraints
1 <= k <= nums.length <= 10^4
-10^4 <= nums[i] <= 10^4
Every case in this set must pass: the first two are visible on Run, the last two unlock on Submit.

Topics

ArraysSortingHeap

Implement kthLargestValue in the editor and return the answer — printing it is never graded.