Kth Largest Value (Set 7)

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 7) — the data. This set runs 4 cases: nums holds arrays of 39–46 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 = [41, 26, -23, 8, 39, 50, 29, 20, 28, 49, 9, 24, 38, -2, 52, 43, -8, 16, 20, 6, -19, 21, 6, 44, 16, 28, 57, -4, 3, 59, 59, 19, 23, -8, 30, -28, -28, 30, 15], k = 1
Output
59

Why

`kthLargestValue` returns `59` for this input.

Example 2

kthLargestValue()
Input
nums = [-4, 24, 34, 11, -8, 18, 55, 53, 60, 49, 34, -6, 15, -23, -13, 25, -20, 54, 57, -29, 33, 50, -15, -7, -25, -19, 11, 25, 21, -2, 7, -23, 59, -17, -15, -3, -6, -13, -19], k = 3
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.