HOTEL VISIT problem give wrong solution with this code

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

int main() {
int n,k,type;
long int t;
cin>>n;
cin>>k;

long arr[n];
int a,b,i;
for(i=0;i<k;i++){
	cin>>type;
	cin>>a;
	cin>>b;
	t=a*a+b*b;
	
	arr[i]=t;
}
sort(arr,arr+k);
while((n-k)!=0){
cin>>type;
if(type==1){cin>>a;
	cin>>b;
	t=a*a+b*b;
	
	if(t<arr[k-1])
	{
		arr[k-1]=t;
		sort(arr,arr+k);
	}
	}
	else if(type==2){
		cout<<arr[k-1]<<endl;
	}

	n--;
}
return 0;

}

Hi @lazy_girl this is a possible case of overflow where aa + bb overflows the maximum possible value of an integer. You should instead use a long long variable.
Here is the code with minor modifications which receives an AC verdict.

1 Like

What is the constraint for int,long and long long(approx in power of 10)?

int and long can go upto 10^9
long long can go upto 10^18
You can do a google search for the exact values.

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.