Kth Largest Value (Set 10)

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

Example 1

kthLargestValue()
Input
nums = [53, 60, -24, 53, -30, 44, 28, 7, 23, 11, -3, 58, 58, 4, 24, 15, 57, 42, 14, -21, -2, -8, -1, -6, 28, 38, -12, -22, 19, 15, 56, 24, 22, -5, -2, 45, 55, 5, 35, 6], k = 2
Output
58

Why

`kthLargestValue` returns `58` for this input.

Example 2

kthLargestValue()
Input
nums = [59, 52, 46, 14, 10, 18, -12, 4, 18, 10, -13, 7, -13, 55, 57, -7, -7, -29, 54, -22, 18, 12, 8, 60, 9, -9, 3, -25, -22, 32, 41, 21, -28, 13, 37, 9, 51, 45, 8, -13, -2, -20, -9, -24, -10, 31, 45], k = 2
Output
59

Why

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