Code not passing test cases and concept not clear

Hostel visit challenge-
I watched the hint video after trying the question and wrote some code but doesn’t pass test cases.

Code- https://ide.codingblocks.com/s/405954

Also the concept is not clear that if we use priority queue how is it going to give us the K th element.

Can you explain the concept with the help of a sample input and tell how the code is working at each step

hello @PranavM16
the question is asking for k nearest point from origin right?

let say u read first k numbers and assume these k number are in ur answer bucket.

now read one new number , this new number can be in ur answer bucket when its value is less than maximum value present in our bucket right?

so in that case what we will do is we throw out the largest value from bucket and insert the newly read value in the bucket.

repeat this process for all remaining unread numbers and in the end we will have our answer
l

Why do we replace the top value of the Priority queue only if the new value is greater than it?

Doesn’t the question simply asks us to print the K th value of whatever the coming input is.
That’s what I wanted to know why we use P.Queue here

we will replace top value if new value is smaller than it (we want nearest and not farthest).

if u put k numbers in max heap(PQ) then the number on the top will be maximum among all k values present (becuase its max heap )

yeah

1 Like

I updated the code with what I understood out of the discussion but it is not working. Can you check this updated code please @aman212yadav

Code- https://ide.codingblocks.com/s/405975

Sample test case is working correctly.
Sample input in the question-
9 3
1 10 10
1 9 9
1 -8 -8
2
1 7 7
2
1 6 6 
1 5 5
2

Sample output-
200
162
98

check now->

1 Like