Hostel visit question

Im getting TLE for one of the test case https://ide.codingblocks.com/s/80307

q can be upto 10^5 and ur time complexity is q^2logq
so u will get a tle
hint—>try to do with priority queue

pop out an element if and only if there are k smaller elemets in the queue

I’ve tried using priority queue still i’m not getting AC https://ide.codingblocks.com/s/81147

I’ve tried using priority queue still i’m not getting AC https://ide.codingblocks.com/s/81147

use long long int instead of int because you are squaring a number and squares wont fit into int data structure.
secondly pop an element when size of queue is greater than k not equal to k.


watch this

1 Like

i have just modified your code.

1 Like

Can you explain your logic? I couldnt get why did you pop and element while printing and not while pushing

you can pop out the element while pushing also or you can pop out the element whenever you want , just the size of queue should not exceed k

1 Like

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.