Kth Largest Value (Set 11)

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

Example 1

kthLargestValue()
Input
nums = [-25, 47, 27, 28, 41, 15, -8, -11, 26, -8, 37, 59, 26, -1, -5, -27, 48, 9, 60, 2, 1, 4, 15, 7, 21, 53, 52, 56, -4, 15, 32, -23, 50, -4, 33, 53, -13, 3, -26, 21, -3, 2, 16, 37, 5, 10, -29], k = 1
Output
60

Why

`kthLargestValue` returns `60` for this input.

Example 2

kthLargestValue()
Input
nums = [6, 59, 6, 2, 25, 33, 57, -29, 35, 29, 17, 12, 11, -5, 41, 23, 17, 21, 38, 31, 27, 47, 4, -29, 11, 23, -16, 45, 5, 11, -16, 30, 11, 22, 51, 12, 45, 54, 60, -25, 59, 47, -19, -16, -22], 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.