Challenge: Find the k Largest Elements in an Array

If you are given an array and any number "k", can you write a code to find the "k" largest elements using a Max Heap?

Problem Statement

In this problem, you have to implement the findKlargest() function to take an array as input and find the “k” largest elements in the array using a Max Heap. An illustration is also provided for your understanding.

Function Prototype

int[] findKLargest(int []arr,int k);

Here, arr is an unsorted integer array and k is an integer.


It returns the integer array containing the first k largest elements from arr

Sample Input

arr = [9,4,7,1,-2,6,5]           k = 3

Sample Output



As “k” is 3, we need to find the top 3 maximum elements from the given array. 9 is the largest value in the array, while 7 is the second-largest, and 6 is the third-largest.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.