Kth Largest Value (Set 12)

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

Example 1

kthLargestValue()
Input
nums = [31, 30, 28, 30, -9, 38, -8, -9, 22, 32, 56, -25, 39, -19, 21, 47, -5, -13, 56, -24, -11, -14, 38, 24, -10, -24, -24, 31, 18, -6, 48, 57, 46, -6, 2, -26, 3, 51, 40, -3, 58, -2, 0, -4, -16, 19, 24], k = 1
Output
58

Why

`kthLargestValue` returns `58` for this input.

Example 2

kthLargestValue()
Input
nums = [-24, -14, 37, 56, 52, 17, 25, 19, 11, 0, 37, 31, 4, 37, 27, -30, 48, 20, -28, -16, 55, -7, 31, -14, -2, 31, 2, 6, 21, 31, 43, -27, 15, 45, -21, 57, 37, -2, 15, -16, 42, 23, 58, 32, 48, 38, 43, 60], 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.