13. K Largest Elements
The problem can be found at the following link: Question Link
My Approach
To find the k
largest elements from a given array, I have used the following approach:
I am using a min-heap implemented as a priority queue to identify the
k
largest elements from the given array.I iterate through the array and insert each element into the priority queue.
If the size of the priority queue exceeds
k
, I remove the smallest element from the queue.After iterating through all the elements, I pop elements from the priority queue and store them in a vector.
Since the elements in the priority queue are in increasing order, I reverse the order of the elements in the vector.
Finally, I return the vector containing the
k
largest elements.
Time and Auxiliary Space Complexity
Time Complexity:
O(n log k)
, wheren
is the size of the array andk
is the number of largest elements required. This is because for each element, we perform an insertion and potential removal operation in the priority queue, which takes O(log k) time.Auxiliary Space Complexity:
O(n)
because we use an additional arrayout
of sizen
and the priority queue (pq) stores at mostk
elements, resulting in an auxiliary space complexity ofO(k)
.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.
Last updated