Dashboard
LC #215

Kth Largest Element in an Array

Pattern · ?MetaAmazonGoogleBloombergMicrosoft

Problem

Find the kth largest element in an unsorted array (not kth distinct).

Constraints

  • 1 ≤ k ≤ nums.length ≤ 10⁵
  • -10⁴ ≤ nums[i] ≤ 10⁴

Example

Input
nums = [3, 2, 1, 5, 6, 4], k = 2
Output
5
Why
Sorted descending: [6,5,4,3,2,1]. 2nd largest is 5.

Spot the pattern

Which pattern cracks it?

Hints

One at a time
Solve on LeetCode ↗