MY test cases are not getting passed I am only getting 20%

if(typ == 1) {

		int x = sc.nextInt();
		int y = sc.nextInt();
		int dis = x*x+y*y;
		pq.add(dis);	
	}

	
	else {
		int[] arr = new int[k-1];
		for(int i = 0; i< k-1; i++) {

			arr[i] = pq.peek();
			pq.remove();
			
		}
		System.out.println(pq.peek());
		for(int i = 0; i< k-1; i++) {
			pq.add(arr[i]);
			
		}

Hey @ayush_r18,

On looking at the constraints we came to realise that the number can vary upto 10^6. So on squaring them the overflow the limit of integer. Thus, we need to use long to contain the data.
Secondly, we can optimise the the type 2 queries. We just need to keep a priority Queue of size k. We can simply keep a max heap containing k elements and it’s root will always be the kth largest distance.

Here’s your updated code.

Hope it helps.

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.