Skip to main content
All Visualizers
#LC 215•Heap / Priority Queue•Medium

Kth Largest Element in an Array

Return the kth largest value in the array.

Understand the problem

Return the kth largest value in the array.

Example 1

Input: {"nums": [3, 2, 1, 5, 6, 4], "k": 2}

Output: 5

Follow the trace to see how the algorithm reaches this result.

Constraints

  • Use the input format shown in the example.
Think first

What information should you keep so you can solve the next step efficiently?

Pattern tags:HeapTop K elements
Time: O(n log k)Space: O(k)
Step 1 of 6 • Use Left/Right arrows or Space to control