Kth Largest Value (Set 4)

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

Example 1

kthLargestValue()
Input
nums = [46, -14, 43, 10, -8, 9, -1, -22, 2, -25, 1, 32, 0, -21, 9, -22, 10, 14, 23, 17, 53, -23, 31, 3, 31, 30, -13], k = 2
Output
46

Why

`kthLargestValue` returns `46` for this input.

Example 2

kthLargestValue()
Input
nums = [24, 1, 41, -29, 8, -1, 51, 20, 56, -6, 59, 24, -5, 59, -14, -27, 31, 7, 4, -22, -26, -3, 22, 36, 14, 54, -15, 11, 24, 3, 18, -9, 6], k = 1
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.