← All problems

Kth Largest Element in an Array

medium

Given an integer array nums and an integer k, return the kth largest element — the kth largest in sorted order, not the kth distinct value.

This is the same "always grab the biggest remaining" idea behind Heap sort, just stopping after k extractions instead of sorting everything.

Example

Input: nums = [3, 2, 1, 5, 6, 4], k = 2
Output: 5
Loading...