Kth Largest Element in Array
Problem Statement
Section titled “Problem Statement”LeetCode #215 - Kth Largest Element in Array
Section titled “LeetCode #215 - Kth Largest Element in Array”Solve this classic coding interview problem efficiently using multiple approaches.
Examples
Section titled “Examples”| nums | k | Output | Explanation |
|---|---|---|---|
[3,2,1,5,6,4] | 2 | 5 | Sorted: [1,2,3,4,5,6]. 2nd largest = 5 |
[3,2,3,1,2,4,5,5,6] | 4 | 4 | Sorted: [1,2,2,3,3,4,5,5,6]. 4th largest = 4 |
[1] | 1 | 1 | Single element |
[7,7,7,7] | 2 | 7 | All same value |
Constraints
Section titled “Constraints”1 <= k <= nums.length <= 10^5-10^4 <= nums[i] <= 10^4
Real-World Applications
Section titled “Real-World Applications”Complexity Comparison
Section titled “Complexity Comparison”| Approach | Time | Space | Best When |
|---|---|---|---|
| Min Heap | O(n log k) | O(k) | Most cases — k is small |
| QuickSelect | O(n) | O(1) | Average case optimal |
| Brute Force (Sort) | O(n log n) | O(1) | Learning or small arrays |
Which approach should you learn first?
Section titled “Which approach should you learn first?”- Pick Min Heap for a balanced, interview-ready solution.
- Pick QuickSelect if you want the theoretical average case optimal solution.
- Pick Brute Force if you are learning the problem from scratch.
Best For Most Cases
Min Heap
O(n log k) time · O(k) space
Optimal Average Case
QuickSelect
O(n) time · O(1) space
Simple to Learn
Brute Force
O(n log n) time · O(1) space
Approach 1: Min Heap (Recommended)
Section titled “Approach 1: Min Heap (Recommended)”Maintain a min heap of size k containing the k largest elements. Iterate through the array and keep only the k largest elements in the heap. The root of the heap is the kth largest.
⏱ Time O(n log k) n iterations, each O(log k) 💾 Space O(k) Heap of k elements
Solution Code
Section titled “Solution Code”from typing import List
def kth_largest_element_in_array_min_heap(nums: List[int]) -> int: """ Kth Largest Element In Array - Min Heap approach.
Time: O(n), Space: O(1) or O(n) depending on approach """ pass
if __name__ == "__main__": # Test cases pass#include <iostream>#include <vector>#include <unordered_map>using namespace std;
class Solution {public: int kth_largest_element_in_array_min_heap(vector<int>& nums) { // Kth Largest Element In Array - Min Heap approach return 0; }};
int main() { return 0;}import java.util.*;
class Solution { public int kth_largest_element_in_array_min_heap(int[] nums) { // Kth Largest Element In Array - Min Heap approach return 0; }
public static void main(String[] args) { // Test cases }}/** * {@param {number[]} nums} * {@return {number}} */var kth_largest_element_in_arrayMinheap = function(nums) { // Kth Largest Element In Array - Min Heap approach return 0;};
if (require.main === module) { // Test cases}pub fn kth_largest_element_in_array_min_heap(nums: Vec<i32>) -> i32 { // Kth Largest Element In Array - Min Heap approach 0}
fn main() { // Test cases}package main
import "fmt"
func kth_largest_element_in_arrayMinheap(nums []int) int { // Kth Largest Element In Array - Min Heap approach return 0}
func main() { // Test cases}Approach 2: QuickSelect
Section titled “Approach 2: QuickSelect”Use the QuickSelect algorithm (based on QuickSort partition) to find the kth largest element without fully sorting. Average time is O(n).
⏱ Time O(n) Average case 💾 Space O(1) Constant
Solution Code
Section titled “Solution Code”from typing import List
def kth_largest_element_in_array_quickselect(nums: List[int]) -> int: """ Kth Largest Element In Array - Quickselect approach.
Time: O(n), Space: O(1) or O(n) depending on approach """ pass
if __name__ == "__main__": # Test cases pass#include <iostream>#include <vector>#include <unordered_map>using namespace std;
class Solution {public: int kth_largest_element_in_array_quickselect(vector<int>& nums) { // Kth Largest Element In Array - Quickselect approach return 0; }};
int main() { return 0;}import java.util.*;
class Solution { public int kth_largest_element_in_array_quickselect(int[] nums) { // Kth Largest Element In Array - Quickselect approach return 0; }
public static void main(String[] args) { // Test cases }}/** * {@param {number[]} nums} * {@return {number}} */var kth_largest_element_in_arrayQuickselect = function(nums) { // Kth Largest Element In Array - Quickselect approach return 0;};
if (require.main === module) { // Test cases}pub fn kth_largest_element_in_array_quickselect(nums: Vec<i32>) -> i32 { // Kth Largest Element In Array - Quickselect approach 0}
fn main() { // Test cases}package main
import "fmt"
func kth_largest_element_in_arrayQuickselect(nums []int) int { // Kth Largest Element In Array - Quickselect approach return 0}
func main() { // Test cases}Approach 3: Brute Force (Sort)
Section titled “Approach 3: Brute Force (Sort)”Sort the array in descending order and return the element at index k-1. Simple and straightforward for learning.
⏱ Time O(n log n) Sorting dominant 💾 Space O(1) Constant
Solution Code
Section titled “Solution Code”from typing import List
def kth_largest_element_in_array_brute_force(nums: List[int]) -> int: """ Kth Largest Element In Array - Brute Force approach.
Time: O(n), Space: O(1) or O(n) depending on approach """ pass
if __name__ == "__main__": # Test cases pass#include <iostream>#include <vector>#include <unordered_map>using namespace std;
class Solution {public: int kth_largest_element_in_array_brute_force(vector<int>& nums) { // Kth Largest Element In Array - Brute Force approach return 0; }};
int main() { return 0;}import java.util.*;
class Solution { public int kth_largest_element_in_array_brute_force(int[] nums) { // Kth Largest Element In Array - Brute Force approach return 0; }
public static void main(String[] args) { // Test cases }}/** * {@param {number[]} nums} * {@return {number}} */var kth_largest_element_in_arrayBruteforce = function(nums) { // Kth Largest Element In Array - Brute Force approach return 0;};
if (require.main === module) { // Test cases}pub fn kth_largest_element_in_array_brute_force(nums: Vec<i32>) -> i32 { // Kth Largest Element In Array - Brute Force approach 0}
fn main() { // Test cases}package main
import "fmt"
func kth_largest_element_in_arrayBruteforce(nums []int) int { // Kth Largest Element In Array - Brute Force approach return 0}
func main() { // Test cases}