Kth Largest Value (Set 8)

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

Example 1

kthLargestValue()
Input
nums = [-25, 5, 48, -25, 26, 29, 12, 7, 25, 34, 8, 49, 53, -23, 29, 29, -24, 59, 37, 19, 30, 43, 21, -15, -25, 5, -1, 25, -7, 15, 4, 52, -24, 59, 58, 55, 8, 59, -8, 47, 8], k = 3
Output
59

Why

`kthLargestValue` returns `59` for this input.

Example 2

kthLargestValue()
Input
nums = [9, 10, 29, 29, 38, -19, -16, -11, 15, 33, 44, -20, 55, 15, 52, 25, -4, 6, 4, -1, 40, -11, -1, -27, 58, 46, 49, -21, -3, -22, 42, 53, -4, 11, 29, 3, 29, -27, 38, 38, 47, 2, 57, 51, 11, 40, 20], k = 1
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.