Can’t understand where I’m going wrong.
Help in debugging
While testing the code on the given example its giving the correct output but not with the testcases. I also cannot understand what approach should I use.
@2304ambikagupta_35a5bee93bbf353e you need to use a priority queue for this problem.
Java Priority Queue
A PriorityQueue is used when the objects are supposed to be processed based on the priority. It is known that a queue follows First-In-First-Out algorithm, but sometimes the elements of the queue are needed to be processed according to the priority, that’s when the PriorityQueue comes into play. The PriorityQueue is based on the priority heap. The elements of the priority queue are ordered according to the natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used.
Read further from here, PriorityQueue.
The following problem can be easily solved by using heaps.
For every query of type 1, insert elements until the size of the heap becomes ‘k’.
Then for every query of type 1 after reaching the size k for heap(max-heap) we will check if the current element is smaller than the root of the heap or not. If it is not smaller then we ignore it else we remove the root of the heap and push the new element in the heap. (What this will do is maintain a heap of size k which will contain k nearest coordinates for the dean) .
For every query of type 2 just print the root of the heap.