Can you please tell me how to excess kth ele?


I am creating array , First I am storing k elements in array then printing kth element from array , then I am putting back k elements in the queue. But it is showing segmentaion fault.
Please tell the mistake, and also how to excess kth element in the queue.

@raj.verma5454,
It is showing segmentation fault, because there might not be k elements, when you are trying to access(and not excess…:rofl:) the kth element, keep a check on that.
I mean you can’t pop more than you have pushed…right…??

oops :frowning: I didn’t notice excess . I can only give input 2 command of printing kth element, when there is atleast k elements in the queue.

Can you tell me an efficient approach , this is showing TLE.

@raj.verma5454,
Hint: At any moment of time, you only need to have k nearest hostels to answer the query, and no more than that, as they are useless. So if you maintain a heap of only k elements then for each query you just need the top of the heap to answer.

Where I am making mistake??

@raj.verma5454,
Your doubt is not clear, but if you are asking why TLE, well that’s because for every query, you would pop O(n) elements from priority_queue. Making total complexity O(n^2.log(n))

You can do it in O(n.log(k))

https://ide.codingblocks.com/s/214266 Please see this code , But some of the test cases are failed.

@raj.verma5454,
Use long long in place of int .

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.