Not able to pass test cases

hello @sid.rajput23
yeah becuase the approach is not efficient

hint -> use max heap to optimise it

why to use max heap?
I have made a minheap of first k distance and when 2 comes I have popped the heap top k-1 times and stored those popped out values in a vector and then I printed the pq.top() and then I pushed back those values from the vector to the heap back

i am not saying ur logic is wrong. it is correct but it is not efficient…

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.

#include <bits/stdc++.h>
using namespace std;

#define int long long 

int32_t main () {
	int n, k; cin >> n >> k;
	priority_queue <int> pq;
	for (int i = 0; i < n; i++) {
		int qtype; cin >> qtype;
		if (qtype == 1) {
			int x, y; cin >> x >> y;
			pq.push(x*x + y*y);
		} else {
			while (pq.size() != k) {
				pq.pop();
			}
			cout << pq.top() << "\n";
		}
	}
	return 0;
}
1 Like

ye approach shayad hi aati dimag me mere
nice one!

but ab nahi bhuloge

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.