VOOZH about

URL: https://www.geeksforgeeks.org/dsa/k-largestor-smallest-elements-in-an-array/

⇱ Find k largest elements in an array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find k largest elements in an array

Last Updated : 23 Jul, 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.


Output
50 30 23 

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.


Output
50 30 23 

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.

Output
50 30 23 

Time Complexity: O(n2) in worst case (O(n) on average).
Auxiliary Space: O(n)

Comment