Kth Largest Value (Set 9)

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 9) — the data. This set runs 4 cases: nums holds arrays of 38–40 items, with values from -29 to 60; k ranges over 2–3. The hidden cases are the largest of the set.

Example 1

kthLargestValue()
Input
nums = [-7, 54, 41, 5, -14, 9, -26, 16, 6, 48, 41, 0, 19, 28, 40, -7, -24, 53, -21, 56, 1, 35, 38, 21, 41, 39, 42, 32, 14, 34, 55, 19, 23, -25, -28, 49, -29, 8], k = 2
Output
55

Why

`kthLargestValue` returns `55` for this input.

Example 2

kthLargestValue()
Input
nums = [45, 19, -22, -14, -18, -17, 23, 46, -6, 60, 56, 37, 26, 55, 46, 31, 22, -2, -22, -4, 47, 40, -22, -12, 36, -24, -12, -14, -23, -5, 14, -27, 52, 28, -4, 13, -3, 32], k = 2
Output
56

Why

`kthLargestValue` returns `56` 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.