What is my mistake


here is the code

Hi @hhiteshbansal_40d12e2460a1738e,

I have corrected your code,

  1. You should use long int to avoid overflow.
    when x = 1e6, x*x = 1e12

  2. priority queue should be passed by reference here else a copy is made and no elements are pushed.

  3. int x=s.top(); change this int x = a.top();

test case 4 is showing time limit excess warning what to do

why is it so what is my mistake

@hhiteshbansal_40d12e2460a1738e,

Your code complexity is O(q*k)
To optimize it, try to maintain k elements in the priority queue at a time. So, every time you push an element in the priority queue check if its size is greater than k or not, if yes then pop to make its size equal to k.

You can check my approach:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.