Kth Largest Element in an Array - Problem

Imagine you're organizing a sports tournament and need to quickly find the k-th best performer from a list of scores. This problem asks you to find the k-th largest element in an unsorted integer array.

Given an integer array nums and an integer k, return the k-th largest element in the array. Note that we want the k-th largest element in sorted order, not the k-th distinct element.

Challenge: Can you solve this without fully sorting the array? There are elegant solutions that can do better than O(n log n) time complexity!

Example: In array [3,2,1,5,6,4] with k=2, the 2nd largest element is 5 (since sorted array would be [6,5,4,3,2,1]).

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [3,2,1,5,6,4], k = 2
โ€บ Output: 5
๐Ÿ’ก Note: The 2nd largest element in the sorted array [6,5,4,3,2,1] is 5
example_2.py โ€” With Duplicates
$ Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
โ€บ Output: 4
๐Ÿ’ก Note: Sorted array: [6,5,5,4,3,3,2,2,1]. The 4th largest element is 4
example_3.py โ€” Single Element
$ Input: nums = [1], k = 1
โ€บ Output: 1
๐Ÿ’ก Note: Only one element exists, so the 1st (and only) largest element is 1

Visualization

Tap to expand
K-th Largest Element: Strategy Comparison๐Ÿ”จ Full SortingO(n log n)โœ“ Simple to implementโœ“ Always worksโœ— Overkill for single element๐Ÿ“š Min-HeapO(n log k)โœ“ Great for small kโœ“ Space efficient O(k)โœ— Still log factorโšก QuickselectO(n) averageโœ“ Optimal average caseโœ“ In-place algorithmโœ— Complex implementationQuickselect Demo: Find 2nd largest in [3,2,1,5,6,4]Step 1: Choose pivot = 5321564Step 2: After partitioning653214Larger than 5Pivot at position 1Smaller than 5๐ŸŽฏ Result: k=2 corresponds to position 1, so answer is 5!
Understanding the Visualization
1
Choose Strategy
Select between full sorting O(n log n), min-heap O(n log k), or quickselect O(n) average
2
Partition/Process
Apply chosen strategy - sort all, maintain k-size heap, or partition around pivots
3
Extract Result
Return the k-th largest element from sorted array, heap root, or pivot position
Key Takeaway
๐ŸŽฏ Key Insight: You don't need to sort everything to find one element. Quickselect partitions strategically around pivots, eliminating half the search space each time, achieving O(n) average performance!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n) average, O(nยฒ) worst case

Average case: each partition reduces problem size by half. Worst case: poor pivot choices lead to O(nยฒ)

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

In-place algorithm, only uses constant extra space for recursion variables

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค k โ‰ค nums.length โ‰ค 105
  • -104 โ‰ค nums[i] โ‰ค 104
  • k is guaranteed to be valid (within array bounds)
Asked in
Facebook 85 Amazon 72 Apple 68 Google 61 Microsoft 55 LinkedIn 43
89.5K Views
Very High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen