Doubt with the hint approach

Although I solved the question in my own approach using multiset. But please also explain to me the apaar sir’s approach. In the hint, he assumed the rocket distances as 1,2,3,4,5,6 and assumed K =4, and popped out all elements greater than 4, but in a real scenario, rocket distances will be something like 50,68,100,1520,200. At that time what does greater than K means? because every element seems greater than K.

you have to output the Rocket distance of Kth nearest hostel till now.
so we have to extract k elements from queue
in the video they assumed the rocket distances as 1,2,3,4,5,6

But if we assume the queue is sorted in descending order as per the video, then extracting K elements mean extracting 4 elements, How can we get the Kth nearest element that is the 4th element.?

while extracting we make a counter
and when counter==k print the element
and this will be kth largest element as we are using priority queue

understood, but according to the question we need to print the Kth nearest element, not the largest

1 Like

yes @ayusdas2000
sorry it’s kth smallest element

i hope your doubt is resolved now