Kth Largest Value (Set 14)

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

Example 1

kthLargestValue()
Input
nums = [35, -8, 12, 41, 8, 33, -13, 17, 54, 1, -24, -6, 17, -12, -17, 5, -15, -3, -3, -10, 60, -29, -18, 50, 45, 52, 9, 39, 25, 24, 1, -18, -23, -18, -13, -2, 38, 34, -19, 16, -11, 48, 40, -4, 22, 29, -15, 51], k = 1
Output
60

Why

`kthLargestValue` returns `60` for this input.

Example 2

kthLargestValue()
Input
nums = [31, -7, 53, 59, 48, 28, 23, 54, 15, 5, -24, 24, 5, -15, 34, 28, -6, -23, 57, 10, 16, 52, -13, -16, 16, -21, -18, 12, 30, -12, -18, 23, 57, 1, 28, 49, 57, 48, 14, -6, 43, -9, 26, 13, 41, 47, -15, 12], k = 2
Output
57

Why

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