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

**URL:** <https://discuss.codechef.com/t/why-is-this-solution-not-accepted-for-bfriend-link-www-codechef-com-problems-bfriend/63614>\
**Category:** Uncategorized\
**Tags:** help, lunch-time\
**Created:** [April 24, 2020, 6:38pm UTC](https://discuss.codechef.com/t/why-is-this-solution-not-accepted-for-bfriend-link-www-codechef-com-problems-bfriend/63614 "2020-04-24T18:38:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon13643412](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@anon13643412](https://discuss.codechef.com/u/anon13643412)\
**Post date:** [April 24, 2020, 6:38pm UTC](https://discuss.codechef.com/t/why-is-this-solution-not-accepted-for-bfriend-link-www-codechef-com-problems-bfriend/63614/1 "2020-04-24T18:38:05Z")

</div>

````auto
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;
}```
````

---

<div class="post-metadata">

**Author:** ![galencolin](https://discuss.codechef.com/user_avatar/discuss.codechef.com/galencolin/32/137557_2.png) [@galencolin](https://discuss.codechef.com/u/galencolin)\
**Post date:** [April 24, 2020, 6:44pm UTC](https://discuss.codechef.com/t/why-is-this-solution-not-accepted-for-bfriend-link-www-codechef-com-problems-bfriend/63614/2 "2020-04-24T18:44:21Z")

</div>

Please [format your code](https://discuss.codechef.com/t/tutorial-color-format-the-code/36585), 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).

---

<div class="post-metadata">

**Author:** ![anon13643412](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@anon13643412](https://discuss.codechef.com/u/anon13643412)\
**Post date:** [April 24, 2020, 6:55pm UTC](https://discuss.codechef.com/t/why-is-this-solution-not-accepted-for-bfriend-link-www-codechef-com-problems-bfriend/63614/3 "2020-04-24T18:55:56Z")

</div>

done

---

<div class="post-metadata">

**Author:** ![galencolin](https://discuss.codechef.com/user_avatar/discuss.codechef.com/galencolin/32/137557_2.png) [@galencolin](https://discuss.codechef.com/u/galencolin)\
**Post date:** [April 24, 2020, 7:06pm UTC](https://discuss.codechef.com/t/why-is-this-solution-not-accepted-for-bfriend-link-www-codechef-com-problems-bfriend/63614/4 "2020-04-24T19:06:33Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![anon13643412](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@anon13643412](https://discuss.codechef.com/u/anon13643412)\
**Post date:** [April 25, 2020, 11:42am UTC](https://discuss.codechef.com/t/why-is-this-solution-not-accepted-for-bfriend-link-www-codechef-com-problems-bfriend/63614/5 "2020-04-25T11:42:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![galencolin](https://discuss.codechef.com/user_avatar/discuss.codechef.com/galencolin/32/137557_2.png) [@galencolin](https://discuss.codechef.com/u/galencolin)\
**Post date:** [April 25, 2020, 1:22pm UTC](https://discuss.codechef.com/t/why-is-this-solution-not-accepted-for-bfriend-link-www-codechef-com-problems-bfriend/63614/6 "2020-04-25T13:22:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon13643412](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@anon13643412](https://discuss.codechef.com/u/anon13643412)\
**Post date:** [April 26, 2020, 11:40am UTC](https://discuss.codechef.com/t/why-is-this-solution-not-accepted-for-bfriend-link-www-codechef-com-problems-bfriend/63614/7 "2020-04-26T11:40:10Z")

</div>

thanks
