How to solve it and what is wrong in my approach

https://practice.geeksforgeeks.org/problems/k-largest-elements3736/1/?category[]=Heap&category[]=priority-queue&company[]=Amazon&company[]=Microsoft&difficulty[]=-1&difficulty[]=0&difficulty[]=1&difficulty[]=2&page=1&query=category[]Heapcategory[]priority-queuecompany[]Amazoncompany[]Microsoftdifficulty[]-1difficulty[]0difficulty[]1difficulty[]2page1#

what is wrong in my approach please help

https://ide.codingblocks.com/s/362696

you should use min heaps

  1. Build a min heap, and start inserting the elements into it.
  2. If heap size becomes equal to K, then compare the incoming element with the element at the top of the heap.
  3. If element at the top is less than the incoming element, then pop the top from heap and insert the new element into the heap.
  4. Finally, you are having K largest elements in the heap. Now, since you need to print the elements in decreasing order, so you can now pop the elements from heap one by one and store it in a vector.

you can refer to this:

what is the use of second parameter in the priority queue min heap implementation