Why is this solution not accepted for BFRIEND? link:https://www.codechef.com/problems/BFRIEND

int main()
{ int t;
scanf("%d",&t);
while(t--){
   long long int n,ind,a,b,c,min=1000000001,time=0,i;
scanf("%lld %lld %lld %lld",&n,&a,&b,&c);
long long int ar[n],m[n];
for(int i=0;i<n;i++)
{
    scanf("%lld",&ar[i]);
    m[i]=(ar[i]-a)*(ar[i]-a)+(ar[i]-b)*(ar[i]-b);

}
for(int i=0;i<n;i++)
{
    if (min>m[i])
    {
        min=m[i];
        ind=i;
    }
}
if (ar[ind]>a)
{
    i=ar[ind]-a;
}
else
    i=a-ar[ind];
if (ar[ind]>b)
{
    min=ar[ind]-b;
}
else
    min=b-ar[ind];

time = c+min+i;
printf("%lld\n",time);
}
    return 0;
}```

Please format your code, or at least link the submission, because just copy/pasting it makes it both ugly and actually messes up the code (for example, -- becomes -, so it’s sometimes impossible to know what you actually meant to do).

1 Like

done

Your initial value for min is too low, a case like this:

1
2 1 2 1
1000000000 100000

will crash your program since the variable ind is never set.

Also, what’s the point in squaring the distances? I think in this particular case, it may not hurt you, but absolute value better represents the actual costs.

1 Like

i thought it will avoid many if else statements needed for absolute value calculations

There’s an abs function (that works with integers and long longs) for exactly that reason.

1 Like

thanks