Find k largest elements in an array
Last Updated :
06 Feb, 2025
Given an array arr[] and an integer k, the task is to find k largest elements in the given array. Elements in the output array should be in decreasing order.
Examples:
Input: [1, 23, 12, 9, 30, 2, 50], k = 3
Output: [50, 30, 23]
Input: [11, 5, 12, 9, 44, 17, 2], k = 2
Output: [44, 17]
[Naive Approach] Using Sorting
The idea is to sort the input array in descending order, so the first k elements in the array will be the k largest elements.
C++
// C++ program to find k largest elements in an
// array using sorting
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> kLargest(vector<int> &arr, int k) {
// sort the given array in descending order
sort(arr.begin(), arr.end(), greater<int>());
// store the first k element in result array
vector<int> res(arr.begin(), arr.begin() + k);
return res;
}
int main() {
vector<int>arr = {1, 23, 12, 9, 30, 2, 50};
int k = 3;
vector<int> res = kLargest(arr, k);
for(int ele : res)
cout << ele << " ";
return 0;
}
Java
// Java program to find k largest elements in an array using
// sorting
import java.util.*;
class GfG {
static ArrayList<Integer> kLargest(int[] arr, int k) {
int n = arr.length;
// Convert int type to Integer
// for sorting with a comparator
Integer[] arrInteger =
Arrays.stream(arr).boxed().toArray(Integer[]::new);
// Sort the array in descending order
Arrays.sort(arrInteger, Collections.reverseOrder());
// Store the first k elements in result list
ArrayList<Integer> res = new ArrayList<>();
for (int i = 0; i < k; i++)
res.add(arrInteger[i]);
return res;
}
public static void main(String[] args) {
int[] arr = {1, 23, 12, 9, 30, 2, 50};
int k = 3;
ArrayList<Integer> res = kLargest(arr, k);
for (int ele : res)
System.out.print(ele + " ");
}
}
Python
# Python program to find k largest elements in an
# array using sorting
def kLargest(arr, k):
# sort the given array in descending order
arr.sort(reverse=True)
# store the first k elements in result list
res = arr[:k]
return res
if __name__ == "__main__":
arr = [1, 23, 12, 9, 30, 2, 50]
k = 3
res = kLargest(arr, k)
print(' '.join(map(str, res)))
C#
// C# program to find k largest elements in an
// array using sorting
using System;
using System.Collections.Generic;
using System.Linq;
class GfG {
static List<int> kLargest(int[] arr, int k) {
// sort the given array in descending order
Array.Sort(arr, (a, b) => b.CompareTo(a));
// store the first k elements in result list
List<int> res = new List<int>();
for (int i = 0; i < k; i++) {
res.Add(arr[i]);
}
return res;
}
static void Main() {
int[] arr = {1, 23, 12, 9, 30, 2, 50};
int k = 3;
List<int> res = kLargest(arr, k);
foreach (int ele in res)
Console.Write(ele + " ");
}
}
JavaScript
// JavaScript program to find k largest elements
// in an array using sorting
function kLargest(arr, k) {
// sort the given array in descending order
arr.sort((a, b) => b - a);
// store the first k elements in result array
let res = arr.slice(0, k);
return res;
}
// Driver Code
const arr = [1, 23, 12, 9, 30, 2, 50];
const k = 3;
const res = kLargest(arr, k);
console.log(res.join(' '));
Time complexity: O(n * log n)
Auxiliary Space: O(1)
[Expected Approach] Using Priority Queue(Min-Heap)
The idea is, as we iterate through the array, we keep track of the k largest elements at each step. To do this, we use a min-heap. First, we insert the initial k elements into the min-heap. After that, for each next element, we compare it with the top of the heap. Since the top element of the min-heap is the smallest among the k elements, if the current element is larger than the top, it means the top element is no longer one of the k largest elements. In this case, we remove the top and insert the larger element. After completing the entire traversal, the heap will contain exactly the k largest elements of the array.
C++
// C++ program to find the k largest elements in the
// array using min heap
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
// Function to find the k largest elements in the array
vector<int> kLargest(vector<int> &arr, int k) {
// Min Priority Queue (Min-Heap) with first k
// elements of the array
priority_queue<int, vector<int>, greater<int>>
minH(arr.begin(), arr.begin() + k);
// Travers n - k elements
for (int i = k; i < arr.size(); i++) {
// If the top of heap is less than the arr[i]
// then remove top element and insert arr[i]
if(minH.top() < arr[i]) {
minH.pop();
minH.push(arr[i]);
}
}
vector<int> res;
// Min heap will contain only k
// largest element
while (!minH.empty()) {
res.push_back(minH.top());
minH.pop();
}
// Reverse the result array, so that all
// elements are in decreasing order
reverse(res.begin(), res.end());
return res;
}
int main() {
vector<int> arr = {1, 23, 12, 9, 30, 2, 50};
int k = 3;
vector<int> res = kLargest(arr, k);
for(int ele : res)
cout << ele << " ";
return 0;
}
Java
// Java program to find the k largest elements in the
// array using min heap
import java.util.*;
class GfG {
// Function to find the k largest elements in the array
static ArrayList<Integer> kLargest(int[] arr, int k) {
// Min-heap to store the k largest elements
PriorityQueue<Integer> minHeap = new PriorityQueue<>(k);
// Add first k elements to the heap
for (int i = 0; i < k; i++) {
minHeap.add(arr[i]);
}
// Traverse the rest of the array
for (int i = k; i < arr.length; i++) {
// If current element is larger than
// the smallest in heap
if (arr[i] > minHeap.peek()) {
minHeap.poll();
minHeap.add(arr[i]);
}
}
// Extract elements from the heap
ArrayList<Integer> res = new ArrayList<>();
while (!minHeap.isEmpty()) {
res.add(minHeap.poll());
}
// Reverse the list for descending order
Collections.reverse(res);
return res;
}
public static void main(String[] args) {
int[] arr = {1, 23, 12, 9, 30, 2, 50};
int k = 3;
ArrayList<Integer> res = kLargest(arr, k);
for (int ele : res) {
System.out.print(ele + " ");
}
}
}
Python
# Python program to find the k largest elements in the
# array using min heap
import heapq
# Function to find the k largest elements in the array
def kLargest(arr, k):
# Create a min-heap with the first k elements
minH = arr[:k]
heapq.heapify(minH)
# Traverse the rest of the array
for x in arr[k:]:
if x > minH[0]:
heapq.heapreplace(minH, x)
res = []
# Min heap will contain only k
# largest element
while minH:
res.append(heapq.heappop(minH))
# Reverse the result array, so that all
# elements are in decreasing order
res.reverse()
return res
if __name__ == "__main__":
arr = [1, 23, 12, 9, 30, 2, 50]
k = 3
res = kLargest(arr, k)
print(" ".join(map(str, res)))
C#
// C# program to find the k largest elements in the
// array using min heap
using System;
using System.Collections.Generic;
class GfG {
// Function to find the k largest elements in the array
static List<int> kLargest(int[] arr, int k) {
// Min-heap using a SortedSet
SortedSet<int> minH = new SortedSet<int>();
// Insert the first k elements into the min-heap
for (int i = 0; i < k; i++) {
minH.Add(arr[i]);
}
// Process the remaining elements
for (int i = k; i < arr.Length; i++) {
// If the current element is larger
// than the smallest in the heap
if (arr[i] > minH.Min) {
minH.Remove(minH.Min);
minH.Add(arr[i]);
}
}
// Extract k largest elements from the heap
List<int> res = new List<int>(minH);
// Reverse the result array to get
// elements in decreasing order
res.Sort((a, b) => b.CompareTo(a));
return res;
}
static void Main() {
int[] arr = { 1, 23, 12, 9, 30, 2, 50 };
int k = 3;
List<int> res = kLargest(arr, k);
foreach (int ele in res)
Console.Write(ele + " ");
}
}
JavaScript
//Driver Code Starts
// JavaScript program to find the k largest elements in the
// array using min heap
class MinHeap {
constructor() {
this.heap = [];
}
// Swap two elements in the heap
swap(i, j) {
[this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]];
}
// Heapify up to maintain min heap property
heapifyUp() {
let index = this.heap.length - 1;
while (index > 0) {
let parentIndex = Math.floor((index - 1) / 2);
if (this.heap[parentIndex] <= this.heap[index]) break;
this.swap(parentIndex, index);
index = parentIndex;
}
}
// Heapify down to maintain min heap property
heapifyDown() {
let index = 0;
while (2 * index + 1 < this.heap.length) {
let leftChild = 2 * index + 1;
let rightChild = 2 * index + 2;
let smallest = leftChild;
if (rightChild < this.heap.length && this.heap[rightChild] < this.heap[leftChild]) {
smallest = rightChild;
}
if (this.heap[index] <= this.heap[smallest]) break;
this.swap(index, smallest);
index = smallest;
}
}
// Insert element into the min heap
push(val) {
this.heap.push(val);
this.heapifyUp();
}
// Remove and return the top element (smallest)
pop() {
if (this.heap.length === 1) return this.heap.pop();
let min = this.heap[0];
this.heap[0] = this.heap.pop();
this.heapifyDown();
return min;
}
// Get the top element (smallest)
top() {
return this.heap[0];
}
// Check if the heap is empty
empty() {
return this.heap.length === 0;
}
}
//Driver Code Ends
// Function to find the k largest elements in the array
function kLargest(arr, k) {
// Min Priority Queue (Min-Heap) with first k
// elements of the array
let minH = new MinHeap();
for (let i = 0; i < k; i++) {
minH.push(arr[i]);
}
// Traverse n - k elements
for (let i = k; i < arr.length; i++) {
// If the top of heap is less than the arr[i]
// then remove top element and insert arr[i]
if (minH.top() < arr[i]) {
minH.pop();
minH.push(arr[i]);
}
}
let res = [];
// Min heap will contain only k
// largest elements
while (!minH.empty()) {
res.push(minH.pop());
}
// Reverse the result array, so that all
// elements are in decreasing order
res.reverse();
return res;
}
//Driver Code Starts
// Driver Code
let arr = [1, 23, 12, 9, 30, 2, 50];
let k = 3;
let res = kLargest(arr, k);
console.log(res.join(" "));
//Driver Code Ends
Time Complexity: O(n * log k), this solution can work in O(k + (n-k) Log K) as build heap take linear time.
Auxiliary Space: O(k)
Note: JavaScript does not seem to support min heap in native implementation, so it is recommended to use Quick Select implementation.
[Alternate Approach] Using Quick Select algorithm
The idea is to use the partitioning step of QuickSort to find the k largest elements in the array, without sorting the entire array. When sorting the element in the descending order, the partitioning step rearranges the elements in a way that all elements greater than or equal to a chosen pivot (usually the last element) are placed on its left, and all elements lesser than the pivot are on its right. And pivot element in its correct sorted position.
After each partition, we compare the number of elements in the leftpart of the array (which contains all elements greater than or equal to the pivot) with k:
- Number of elements in the left = k, it means all elements in the left part (including pivot) are the k largest elements.
- Number of elements in the left > k, it means that k largest elements exist in the left subarray only, so we recursively search in the left subarray.
- Number of elements in the left < k, it means that the k largest elements include the entire left part of the array along with some elements from the right part. Therefore we reduce k by the number of elements already covered on the left side and search in the right subarray.
C++
// C++ program to find the k largest elements in the array
// using partitioning step of quick sort
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Function to partition the array around a pivot
int partition(vector<int> &arr, int left, int right) {
// Last element is chosen as a pivot.
int pivot = arr[right];
int i = left;
for (int j = left; j < right; j++) {
// Elements greater than or equal to pivot
// are placed in the left side of pivot
if (arr[j] >= pivot) {
swap(arr[i], arr[j]);
i++;
}
}
swap(arr[i], arr[right]);
// The correct sorted position of the pivot
return i;
}
void quickSelect(vector<int> &arr, int left, int right, int k) {
if (left <= right) {
int pivotIdx = partition(arr, left, right);
// Count of all elements in the left part
int leftCnt = pivotIdx - left + 1;
// If leftCnt is equal to k, then we have
// found the k largest element
if (leftCnt == k)
return;
// Search in the left subarray
if (leftCnt > k)
quickSelect(arr, left, pivotIdx - 1, k);
// Reduce the k by number of elements already covered
// and search in the right subarray
else
quickSelect(arr, pivotIdx + 1, right, k - leftCnt);
}
}
vector<int> kLargest(vector<int> &arr, int k) {
int n = arr.size();
quickSelect(arr, 0, n - 1, k);
// First k elements of the array will be the largest
vector<int> res(arr.begin(), arr.begin() + k);
// Sort the result in descending order
sort(res.begin(), res.end(), greater<int>());
return res;
}
int main() {
vector<int> arr = {1, 23, 12, 9, 30, 2, 50};
int k = 3;
vector<int> res = kLargest(arr, k);
for (int ele : res)
cout << ele << " ";
return 0;
}
Java
// Java program to find the k largest elements in the array
// using partitioning step of quick sort
import java.util.*;
class GfG {
// Function to partition the array around a pivot
static int partition(int[] arr, int left, int right) {
// Last element is chosen as a pivot.
int pivot = arr[right];
int i = left;
for (int j = left; j < right; j++) {
// Elements greater than or equal to pivot
// are placed in the left side of pivot
if (arr[j] >= pivot) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}
}
int temp = arr[i];
arr[i] = arr[right];
arr[right] = temp;
// The correct sorted position of the pivot
return i;
}
static void quickSelect(int[] arr, int left, int right, int k) {
if (left <= right) {
int pivotIdx = partition(arr, left, right);
// Count of all elements in the left part
int leftCnt = pivotIdx - left + 1;
// If leftCnt is equal to k, then we have
// found the k largest element
if (leftCnt == k)
return;
// Search in the left subarray
if (leftCnt > k)
quickSelect(arr, left, pivotIdx - 1, k);
// Reduce the k by number of elements already covered
// and search in the right subarray
else
quickSelect(arr, pivotIdx + 1, right, k - leftCnt);
}
}
static ArrayList<Integer> kLargest(int[] arr, int k) {
quickSelect(arr, 0, arr.length - 1, k);
ArrayList<Integer> res = new ArrayList<>();
// First k elements of the array, will be the largest
for(int i = 0; i < k; i++)
res.add(arr[i]);
// Sort the result in descending order
Collections.sort(res, Collections.reverseOrder());
return res;
}
public static void main(String[] args) {
int[] arr = {1, 23, 12, 9, 30, 2, 50};
int k = 3;
ArrayList<Integer> res = kLargest(arr, k);
for (int ele : res)
System.out.print(ele + " ");
}
}
Python
# Python program to find the k largest elements in the array
# using partitioning step of quick sort
# Function to partition the array around a pivot
def partition(arr, left, right):
# Last element is chosen as a pivot.
pivot = arr[right]
i = left
for j in range(left, right):
# Elements greater than or equal to pivot
# are placed in the left side of pivot
if arr[j] >= pivot:
arr[i], arr[j] = arr[j], arr[i]
i += 1
arr[i], arr[right] = arr[right], arr[i]
# The correct sorted position of the pivot
return i
def quickSelect(arr, left, right, k):
if left <= right:
pivotIdx = partition(arr, left, right)
# Count of all elements in the left part
leftCnt = pivotIdx - left + 1
# If leftCnt is equal to k, then we have
# found the k largest element
if leftCnt == k:
return
# Search in the left subarray
if leftCnt > k:
quickSelect(arr, left, pivotIdx - 1, k)
# Reduce the k by number of elements already covered
# and search in the right subarray
else:
quickSelect(arr, pivotIdx + 1, right, k - leftCnt)
def kLargest(arr, k):
quickSelect(arr, 0, len(arr) - 1, k)
# First k elements of the array, will be the largest
res = arr[:k]
# Sort the result in descending order
res.sort(reverse=True)
return res
if __name__ == "__main__":
arr = [1, 23, 12, 9, 30, 2, 50]
k = 3
res = kLargest(arr, k)
print(" ".join(map(str, res)))
C#
// C# program to find the k largest elements in the array
// using partitioning step of quick sort
using System;
using System.Collections.Generic;
class GfG {
// Function to partition the array around a pivot
static int partition(int[] arr, int left, int right) {
// Last element is chosen as a pivot.
int pivot = arr[right];
int i = left;
for (int j = left; j < right; j++) {
// Elements greater than or equal to pivot
// are placed in the left part of pivot
if (arr[j] >= pivot) {
swap(arr, i, j);
i++;
}
}
swap(arr, i, right);
// The correct sorted position of the pivot
return i;
}
static void quickSelect(int[] arr, int left, int right, int k) {
if (left <= right) {
int pivotIdx = partition(arr, left, right);
// Count of all elements in the left part
int leftCnt = pivotIdx - left + 1;
// If leftCnt is equal to k, then we have
// found the k largest element
if (leftCnt == k)
return;
// Search in the left subarray
if (leftCnt > k)
quickSelect(arr, left, pivotIdx - 1, k);
// Reduce the k by number of elements already covered
// and search in the right subarray
else
quickSelect(arr, pivotIdx + 1, right, k - leftCnt);
}
}
static List<int> kLargest(int[] arr, int k) {
quickSelect(arr, 0, arr.Length - 1, k);
// First k elements of the array will be the largest
List<int> res = new List<int>();
for (int i = 0; i < k; i++) {
res.Add(arr[i]);
}
// Sort the list in descending order
res.Sort((a, b) => b.CompareTo(a));
return res;
}
static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static void Main() {
int[] arr = {1, 23, 12, 9, 30, 2, 50};
int k = 3;
List<int> res = kLargest(arr, k);
foreach (int ele in res)
Console.Write(ele + " ");
}
}
JavaScript
// JavaScript program to find the k largest elements in the array
// using partitioning step of quick sort
// Function to partition the array around a pivot
function partition(arr, left, right) {
// Last element is chosen as a pivot.
let pivot = arr[right];
let i = left;
for (let j = left; j < right; j++) {
// Elements greater than or equal to pivot are
// placed in the left part of pivot
if (arr[j] >= pivot) {
[arr[i], arr[j]] = [arr[j], arr[i]];
i++;
}
}
[arr[i], arr[right]] = [arr[right], arr[i]];
// The correct sorted position of the pivot
return i;
}
function quickSelect(arr, left, right, k) {
if (left <= right) {
let pivotIdx = partition(arr, left, right);
// Count of all elements in the left part
let leftCnt = pivotIdx - left + 1;
// If leftCnt is equal to k, then the first
// k element of the array will be largest
if (leftCnt === k)
return;
// Search in the left subarray
if (leftCnt > k)
quickSelect(arr, left, pivotIdx - 1, k);
// Reduce the k by number of elements already covered
// and search in the right subarray
else
quickSelect(arr, pivotIdx + 1, right, k - leftCnt);
}
}
function kLargest(arr, k) {
quickSelect(arr, 0, arr.length - 1, k);
// First k elements of the array, will be the largest
let res = arr.slice(0, k);
// Sort the first k elements in descending order
res.sort((a, b) => b - a);
return res;
}
// Driver Code
const arr = [1, 23, 12, 9, 30, 2, 50];
const k = 3;
const res = kLargest(arr, k);
console.log(res.join(' '));
Time Complexity: O(n2) in worst case (O(n) on average).
Auxiliary Space: O(n)
Similar Reads
Heap Data Structure A Heap is a complete binary tree data structure that satisfies the heap property: for every node, the value of its children is greater than or equal to its own value. Heaps are usually used to implement priority queues, where the smallest (or largest) element is always at the root of the tree.Basics
2 min read
Introduction to Heap - Data Structure and Algorithm Tutorials A Heap is a special tree-based data structure with the following properties:It is a complete binary tree (all levels are fully filled except possibly the last, which is filled from left to right).It satisfies either the max-heap property (every parent node is greater than or equal to its children) o
15+ min read
Binary Heap A Binary Heap is a complete binary tree that stores data efficiently, allowing quick access to the maximum or minimum element, depending on the type of heap. It can either be a Min Heap or a Max Heap. In a Min Heap, the key at the root must be the smallest among all the keys in the heap, and this pr
13 min read
Advantages and Disadvantages of Heap Advantages of Heap Data StructureTime Efficient: Heaps have an average time complexity of O(log n) for inserting and deleting elements, making them efficient for large datasets. We can convert any array to a heap in O(n) time. The most important thing is, we can get the min or max in O(1) timeSpace
2 min read
Time Complexity of building a heap Consider the following algorithm for building a Heap of an input array A. A quick look over the above implementation suggests that the running time is O(n * lg(n)) since each call to Heapify costs O(lg(n)) and Build-Heap makes O(n) such calls. This upper bound, though correct, is not asymptotically
2 min read
Applications of Heap Data Structure Heap Data Structure is generally taught with Heapsort. Heapsort algorithm has limited uses because Quicksort is better in practice. Nevertheless, the Heap data structure itself is enormously used. Priority Queues: Heaps are commonly used to implement priority queues, where elements with higher prior
2 min read
Comparison between Heap and Tree What is Heap? A Heap is a special Tree-based data structure in which the tree is a complete binary tree. Types of Heap Data Structure: Generally, Heaps can be of two types: Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys present at all of its children. The sa
3 min read
When building a Heap, is the structure of Heap unique? What is Heap? A heap is a tree based data structure where the tree is a complete binary tree that maintains the property that either the children of a node are less than itself (max heap) or the children are greater than the node (min heap). Properties of Heap: Structural Property: This property sta
4 min read
Some other type of Heap
Easy problems on Heap
Check if a given Binary Tree is a HeapGiven a binary tree, check if it has heap property or not, Binary tree needs to fulfil the following two conditions for being a heap: It should be a complete tree (i.e. Every level of the tree, except possibly the last, is completely filled, and all nodes are as far left as possible.).Every nodeâs v
15+ min read
How to check if a given array represents a Binary Heap?Given an array, how to check if the given array represents a Binary Max-Heap.Examples: Input: arr[] = {90, 15, 10, 7, 12, 2} Output: True The given array represents below tree 90 / \ 15 10 / \ / 7 12 2 The tree follows max-heap property as every node is greater than all of its descendants. Input: ar
11 min read
Iterative HeapSortHeapSort is a comparison-based sorting technique where we first build Max Heap and then swap the root element with the last element (size times) and maintains the heap property each time to finally make it sorted. Examples: Input : 10 20 15 17 9 21 Output : 9 10 15 17 20 21 Input: 12 11 13 5 6 7 15
11 min read
Find k largest elements in an arrayGiven an array arr[] and an integer k, the task is to find k largest elements in the given array. Elements in the output array should be in decreasing order.Examples:Input: [1, 23, 12, 9, 30, 2, 50], k = 3Output: [50, 30, 23]Input: [11, 5, 12, 9, 44, 17, 2], k = 2Output: [44, 17]Table of Content[Nai
15+ min read
Kâth Smallest Element in Unsorted ArrayGiven an array arr[] of N distinct elements and a number K, where K is smaller than the size of the array. Find the K'th smallest element in the given array. Examples:Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 3 Output: 7Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 4 Output: 10 Table of Content[Naive Ap
15 min read
Height of a complete binary tree (or Heap) with N nodesConsider a Binary Heap of size N. We need to find the height of it. Examples: Input : N = 6 Output : 2 () / \ () () / \ / () () () Input : N = 9 Output : 3 () / \ () () / \ / \ () () () () / \ () ()Recommended PracticeHeight of HeapTry It! Let the size of the heap be N and the height be h. If we tak
3 min read
Heap Sort for decreasing order using min heapGiven an array of elements, sort the array in decreasing order using min heap. Examples: Input : arr[] = {5, 3, 10, 1}Output : arr[] = {10, 5, 3, 1}Input : arr[] = {1, 50, 100, 25}Output : arr[] = {100, 50, 25, 1}Prerequisite: Heap sort using min heap.Using Min Heap Implementation - O(n Log n) Time
11 min read