Bob and His Friends Codechef Solution

Hello Programmers In this post, you will know how to solve the Bob and His Friends Codechef Solution. The Problem Code: BFRIEND.

Bob and His Friends Codechef Solution
Bob and His Friends Codechef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Problem

Alice just invited Bob to come over for dinner at her place. Bob is not dressed properly and he does not wish to take any chances, so he wants to rush to an apartment of one of his N friends, change there and meet Alice for the dinner.

Alice and Bob’s friends live in a skyscraper with many floors. Alice lives on the a-th floor, the apartments of Bob’s friends are located on the floors F1,F2,…,FN and Bob is initially at the b-th floor. It takes exactly 1 minute to go one floor up or down from each floor.

Bob needs exactly c minutes to change in any of his friends’ apartments. Find the minimum time he needs to go to one of his friends’ apartments, change and get to Alice’s apartment.

Input

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains four space-separated integers N, a, b and c.
  • The second line contains NN space-separated integers F1,F2,…,FN.

Output

Print a single line containing one integer ― the minimum time Bob needs.

Constraints

  • 1≤T≤101≤T≤10
  • 1≤N≤1051≤N≤105
  • 1≤a,b,c≤1091≤a,b,c≤109
  • 1≤Fi≤1091≤Fi≤109 for each valid i

Example

Sample Input 1 

2
3 1 5 2
6 7 8
1 1 2 1000000000
1000000000

Sample Output 1 

8
2999999997

Bob and His Friends CodeChef Solutions in JAVA

import java.util.Arrays;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int tc = 0; tc < T; ++tc) {
			int N = sc.nextInt();
			int a = sc.nextInt();
			int b = sc.nextInt();
			int c = sc.nextInt();
			int[] F = new int[N];
			for (int i = 0; i < F.length; ++i) {
				F[i] = sc.nextInt();
			}
			System.out.println(solve(F, a, b, c));
		}
		sc.close();
	}
	static long solve(int[] F, int a, int b, int c) {
		return Arrays.stream(F).mapToLong(x -> (long) Math.abs(x - a) + Math.abs(x - b) + c).min().getAsLong();
	}
}

Bob and His Friends CodeChef Solution in CPP

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int t;cin>>t;
	while(t--){
	    ll n,a,b,c;cin>>n>>a>>b>>c;
        ll mini=LONG_LONG_MAX;
        for(ll i=0;i<n;i++){
            ll f;cin>>f;
            ll total=max(f,a)-min(f,a)+max(f,b)-min(f,b)+c;
            mini=min(mini,total);
        }
        cout<<mini<<"\n";
    }
    return 0;
}

Bob and His Friends CodeChef Solutions in Python

import sys
t=int(input())
for i in range(t):
    s=input().split()
    n=int(s[0])
    a=int(s[1])
    b=int(s[2])
    c=int(s[3])
    l=list(map(int,input().split()))
    t2=sys.maxsize
    for j in l:
        t1=abs(a-j)+abs(b-j)+c
        t2=min(t1,t2)
    print(t2)
Ezoicreport this ad

Disclaimer: The above Problem (Bob and His Friends) is generated by CodeChef but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: 1 – Cakezoned Codechef Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad