Problem LeetCode 215. Kth Largest Element in an Array ↗ · NeetCode ↗· The solutions on this page are my own.

Kth Largest Visualizer

Four solutions to LeetCode 215, run step by step next to their Java code. Pick a variant and an array, or type your own, then step through the run.

The problem, step by step

Skip to the visualizer ↓

Find the kth largest number in an array

You get an array of numbers and a number k. Picture the array ordered from largest to smallest: the answer is whatever sits at place k.

  1. What you get

    An integer array nums and an integer k, where k is at least 1 and at most the length of the array. Values can repeat.

  2. What you return

    One number: the value at place k when the array is ordered from largest to smallest. The value itself, not its position.

  3. The rules

    • Duplicates each take their own place. In [9, 9, 7] the 1st and 2nd largest are both 9, and the 3rd largest is 7. It is the kth largest element, not the kth largest distinct value.
    • k = 1 asks for the maximum, and k equal to the length asks for the minimum.
    • Nothing says the array has to stay as it is, so you may reorder it.
  4. Try it by hand

    Input
    nums = [7, 2, 9, 4, 9, 1], k = 3
    Output
    7
    as given
    largest first, with its place
    1. Order the numbers from largest to smallest: 9, 9, 7, 4, 2, 1.
    2. Count places from the left: the first 9 is 1st, the second 9 is 2nd, and 7 is 3rd.
    3. Place 3 holds 7, so the answer is 7.
    4. The same answer the other way round: sorted smallest first, [1, 2, 4, 7, 9, 9], the kth largest sits at index n − k = 6 − 3 = 3, which holds 7.
  5. Edge cases to check

    • One element: [5] with k = 1 gives 5.
    • All values equal: [4, 4, 4] gives 4 for any k.
    • Negative numbers: [-1, -5, -3] with k = 2 gives -3.
    • Duplicates around the answer: [2, 2, 3, 3, 3] with k = 4 gives 2, because the three 3s fill places 1 to 3.
  6. The question behind the solutions

    Sorting everything answers it, but you only need one place. When k is small, do you have to order the whole array? And can you land on place k without ordering the numbers you will never look at?

Four ways to reach place k

The example from above, nums = [7, 2, 9, 4, 9, 1] and k = 3, sketched the way you would draw it in an interview. Each board shows what the approach keeps and how it lands on 7. Then step through all four below.

1. Sort KthLargestSort

Remembers: nothing extra; it sorts the array.

nums729491sort, smallest first124799012345index n − k = 6 − 3 = 3nums[3] = 7 ✓
  1. Sort the array, smallest first.
  2. The kth largest sits at index n − k. Return it.

The catch: O(n log n) to put all n values in order, when you only ever read one place.

2. Min-heap of size k KthLargestMinHeap

Remembers: the k largest values seen so far, smallest on top.

729491kept or polledmin-heap, size 3799+7, +2, +9: size 3+4: size 4 > 3, poll 2+9: poll 4+1: poll 1root 7 = 3rd largest ✓
  1. Offer each value to the heap.
  2. When it holds more than k, poll the root: the smallest cannot be among the k largest.
  3. At the end, the root is the kth largest.

The catch: O(n log k) time and O(k) memory. Great when k is small, but with k close to n it is no better than sorting.

3. Quickselect KthLargestQuickselect

Remembers: the array itself, reordered in place, and the range lo to hi still in play.

target: index n − k = 3, marked ★124997★pivot 4:★ is on the right124799★pivot 9:★ is on the left124799★pivot 7:★ landed on it ✓less= pivotgreater
  1. The target is index n − k. Pick a random pivot in [lo, hi].
  2. Partition the range into less, equal and greater than the pivot.
  3. Target inside the equal block: return the pivot. Otherwise keep only the side that holds the target.

The catch: O(n) on average, but a run of unlucky pivots makes it O(n²). A random pivot makes that very unlikely. It also reorders your input.

4. Counting KthLargestCounting

Remembers: one count for every value from min to max.

nums729491count each value110100102123456789valuewalk down from 9 with k = 3:9: two of them, 3 − 2 = 1 left8: none, skip7: one, 1 − 1 = 0, so the answer is 7 ✓
  1. Find min and max, then count how often each value occurs.
  2. Walk the values from max down, taking each count off k.
  3. When k reaches 0 or below, that value is the answer.

The catch: O(n + range) time and O(range) memory. Fine for values 1 to 9, hopeless when they span a billion.

Four ways to solve it, step by step

Algorithm
Preset

0
0
0
?answer
nums

lines that run are highlighted

      

Complexity · n values, range r = max − min + 1

time
extra space

All four compared

VariantTimeWorstExtra spaceChanges numsBest when

Related in the Lab PriorityQueue in the Java Collections Field Guide

© 2026 Arman Ajabkhani · armantik.com · Privacy