Kth Largest Value (Set 6)

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 6) — the data. This set runs 4 cases: nums holds arrays of 27–42 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 = [-25, -6, 37, 52, -16, 17, -15, -9, -13, -1, 33, -6, -6, -14, 0, 41, 60, 55, -6, 50, -9, -8, 8, 6, -1, 1, 58], k = 3
Output
55

Why

`kthLargestValue` returns `55` for this input.

Example 2

kthLargestValue()
Input
nums = [11, 1, 48, -7, 43, 25, 12, 15, -20, 53, 38, -29, -4, 50, 52, 24, 24, 39, 46, 5, 18, 58, 37, -6, 41, 60, 17, 51, -12, -18, 60, 46, -15, -19, 58, -16, 4, 17, 16, 9, -9, 10], k = 2
Output
60

Why

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