BFRIEND - Editorial

This one

#include
using namespace std;

int main() {
// your code goes here
int T;
cin>>T;
while(T–)
{
long long int N,a,b,c;
cin>>N>>a>>b>>c;
long long int F[N],mi=99999999999999999,k=0;
for(int i=0;i<N;i++)
{
cin>>F[i];
if(abs(F[i]-b)<mi)
{
k=F[i];mi=abs(F[i]-b);
}
}long long int re=mi+abs(k-a)+c;
cout<<re<<endl;

}
return 0;

}
whats wrong with this code

Please either format your code or link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

it has passed the first test case
but giving a WA

Why my code is showing RUN TIME ERROR?

#include <stdio.h>

#include <stdlib.h>

int main()

{

long int choice;

scanf("%d", &choice); // number of testcases

long long int n, alice, bob, time;

while (choice)

{

    scanf("%ld", &n);

    scanf("%lld", &alice);

    scanf("%lld", &bob);

    scanf("%lld", &time);

    long long int friends[n];

    for (int i = 0; i < n; i++)

    {

        scanf("%lld", &friends[i]);

    }

    long long int min = friends[0];

    for (int i = 1; i < n; i++)

    {

        if (friends[i] < min)

        {

            min = friends[i];

        }

    }

    long long int total_time = 2 * (min - alice) + time + (bob - alice);

    printf("%lld", total_time);

        choice--;

}



return 0;

}

@ssjgz Please look at my code and correct me the given test cases are passed but when i’m trying to submit it i’m getting WA :frowning:

public static void main (String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test = Integer.parseInt(br.readLine());
while(test-- > 0){
String str = br.readLine();
String[] arr = str.split("\s+");
int n = Integer.parseInt(arr[0]);
int a = Integer.parseInt(arr[1]);
int b = Integer.parseInt(arr[2]);
int c = Integer.parseInt(arr[3]);
str = br.readLine();
arr = str.split("\s+");
long result = Integer.MAX_VALUE;
long UP = Integer.MAX_VALUE;
long DOWN = Integer.MIN_VALUE;
while(n > 0){
int l = Integer.parseInt(arr[–n]);
UP= (Math.abs(l-a))+(Math.abs(l-b));
DOWN = (Math.abs(a-l))+(Math.abs(b-l));
result = Math.min((UP+c),(DOWN+c));
}
System.out.println(result);
}
}

Your solution: CodeChef: Practical coding for everyone fails on the testcase:

1
3 17 67 4
72 52 78
1 Like

Please check this code

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

int main()
{
int t;
long long int N,a,b,c,x;
cin>>t;
for(long long int i=0;i<t;i++)
{
cin>>N>>a>>b>>c;
long long int A[N],x;
for(long long int k=0;k<N;k++)
{
cin>>A[k];
}
long long int min = abs(b - A[0]);
long long int f = A[0];
for(long long int m=1;m<N;m++)
{
x=abs(b-A[m]);
if(min>x)
{
f=A[m];
}
}
long long int y = abs(f - b) + c + abs(f - a);
cout<<y<<endl;
}
return 0;
}