215. Kth Largest Element in an Array

Kth Largest Element in an Array (LeetCode 215) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Heap (Priority Queue) Patterns pattern, in the Top K Elements (Selection/Frequency) subpattern.

How to solve Kth Largest Element in an Array

Kth Largest Element in an Array is a Top K Elements problem, part of Heap (Priority Queue) Patterns. Find top K elements using heap. Solve kth largest, top K frequent, and k closest points problems.

Work towards the solution in these steps:

  1. Think about data structures that can efficiently keep track of the largest or smallest elements as you process the array.
  2. A min-heap can help maintain the k largest elements seen so far by always keeping the smallest of those k elements at the root.
  3. Iterate through the array and add elements into the min-heap; if the heap size exceeds k, remove the smallest element to ensure only the k largest remain.

The reference solution runs in O(n) time and O(1) space.

The Kth Largest Element in an Array solution and editorial walks through the full approach with reference code.

Problem statement

Given an integer array nums and an integer k, return the kth largest element in the array.

Note that it is the kth largest element in the sorted order, not the kth distinct element.

Can you solve it without sorting?

Example 1:

Input: nums = [3,2,1,5,6,4], k = 2 Output: 5

Example 2:

Input: nums = [3,2,3,1,2,4,5,5,6], k = 4 Output: 4

Constraints:

- 1 <= k <= nums.length <= 10^5

- -10^4 <= nums[i] <= 10^4

Topics and companies

Topics: Array, Divide and Conquer, Sorting, Heap (Priority Queue), Quickselect.

Reported in interviews at Adobe, Amazon, Apple, Bloomberg, ByteDance, Facebook, Goldman Sachs, Google, LinkedIn, Microsoft, Spotify, Walmart Labs and others.

How to practise Kth Largest Element in an Array on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.

Open Kth Largest Element in an Array in the code editor, or read the Kth Largest Element in an Array editorial for a worked solution with its approach and complexity analysis.

Where Kth Largest Element in an Array sits in the DSA pattern sheet

Problems related to Kth Largest Element in an Array

Other problems that use the same Top K Elements (Selection/Frequency) and Heap (Priority Queue) Patterns techniques:

Kth Largest Element in an Array FAQ

What is the LeetCode number of Kth Largest Element in an Array?

Kth Largest Element in an Array is LeetCode problem 215, rated Medium.

What pattern is used to solve Kth Largest Element in an Array?

Kth Largest Element in an Array is a Top K Elements problem. It belongs to the Heap (Priority Queue) Patterns pattern, in the Top K Elements (Selection/Frequency) subpattern.

What is the time complexity of Kth Largest Element in an Array?

The reference solution runs in O(n) time and O(1) space.

Which companies ask Kth Largest Element in an Array?

Kth Largest Element in an Array has been reported in interviews at Adobe, Amazon, Apple, Bloomberg, ByteDance, Facebook, Goldman Sachs, Google, LinkedIn, Microsoft, Spotify, Walmart Labs and others.

Preparing this workspace.