"WA" when int, "AC" when long long int

hello everyone, while solving this question , I took an array arr[n] of int because the numbers lie in range of 10^-5 and 10^5…I think the numbers can easily fit in int…but it gives me WA … but when i change it to long long int , it gives me AC…why so??

My submission : Submission #19424524 - AtCoder Beginner Contest 180
Problem Link : B - Various distances

Code :

void solve()
{
long long int n;
cin>>n;

**int arr[n];**// when i change it to long long int, it gives me AC, but why? since the constraints are not big, it should have worked fine with int also

for(lli i=0;i<n;i++)
   cin>>arr[i];
long long int m=0;
long long int e=0;
int  c=0;
for(lli i=0;i<n;i++)
{   m+=abs(arr[i]);
    e+=arr[i]*arr[i];
    c=max(c,abs(arr[i]));
}
 double E=(double)(pow(e,0.5));
cout<<m<<endl;
cout<<E<<endl;
cout<<c<<endl;

}

Euclidean distance takes square of every coordinate. If you square 10^5, it will result in overflow.

1 Like

but i have store the sqaured value in long long int e

try this

int a = 1e8;
long long int val = a*a;
cout<<val;
2 Likes

What you are seeing is called type casting. Product of two ints is also stored in int. You need to explicitly cast them to long long. Like this

long long e = (long long) a * b;

Here we are casting a to long long explicitly and b will get cased to long long implicitly. Then the product will be stored in e.

1 Like

i got it…thanks a lot