Hostel Visit attempt at the question

#include<bits/stdc++.h>

using namespace std;

int main(){

int test,key;
cin>>test>>key;

int  m[test][3];
int i=0;	
while(test --){
	int query;
	cin >> query;
	if(query == 1){
		m[i][0] = query;
		int x,y;
		cin>>x>>y;
		m[i][1]=x;
		m[i][2]=y;
	}
	else if(query == 2){
		m[i][0]=query;
		m[i][1]=0;
		m[i][2]=0;
	}
	i++;
}
vector<int> ans;
for(int i=0 ; i<test; i++){
	if(m[i][0] == 1){
		int RD = abs(pow(m[i][1],2))+abs(pow(m[i][2],2));
		ans.insert(ans.begin(), RD);
	}
	else{
		cout<<ans[k-1];<<endl;
	}
	
}

return 0;

}

Please help me with this code

Hey @akshpreetsinghs

You are not maintaining kth farthest node
You are simply adding distance to vector without keeping them in sorted order
And if u sort the array then it will give u TLE

Try to think in terms of heap.
Refer to this for approach if reqd : Can you please tell where i m going wrong

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.