Food Balance
Source: codechef
Difficulty: 215

Food Balance
Chef is preparing to cook his dinner.
There are two dishes Chef can make. The first one contains$F_1$grams of fat and$P_1$grams of protein, while the second contains$F_2$grams of fat and$P_2$grams of protein.
Chef would like the quantity of fats and proteins he consumes to be as close to each other as possible, i.e., the absolute difference between the amount of fats and proteins should be as small as possible.
Help Chef by telling him which dish he'll choose; or if both dishes have the same difference.
Input Format
The first and only line of input contains four space-separated integers$F_1, P_1, F_2,$and$P_2$— the quantities of fat and protein in the first dish and second dish, respectively.
Output Format
Output a single string:
"First"
(without quotes) if Chef will choose the first dish.
"Second"
(without quotes) if Chef will choose the second dish.
"Both"
(without quotes) if both dishes are equivalent.
Each character of the output may be in either uppercase or lowercase, i.e. if the answer is
Both
, then any of the strings
BOTH
,
both
,
Both
,
bOTh
, and so on will be accepted.
Constraints$1 \leq F_1, P_1, F_2, P_2 \leq 100$Sample 1:
Explanation:
The first dish has a difference of$|30 - 40| = 10$between its fats and proteins, while the second dish has a difference of$|35 - 44| = 9$.
Chef will choose the second dish, since it has a smaller difference.
Sample 2:
Input
Output
1 100 100 1
Both
Explanation:
The first dish has a difference of$|1 - 100| = 99$between its fats and proteins, while the second dish has a difference of$|100 - 1| = 99$.
Both dishes have the same difference.
Sample 3:
Input
Output
58 56 38 52
First
Explanation:
The first dish has a difference of$|58 - 56| = 2$between its fats and proteins, while the second dish has a difference of$|38 - 52| = 14$.
Chef will choose the first dish, since it has a smaller difference.

Sample Input:
30 40 35 44

Sample Output:
Second

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

int main() {
    // your code goes here
int f1,p1,f2,p2;
cin>>f1>>p1>>f2>>p2;
if(abs(f1-p1)<abs(f2-p2)){
    cout<<"First";
}
else if(abs(f1-p1) >abs(f2-p2)){
    cout<<"Second";
}
else{
    cout<<"Both";
}
}

Go solution:
package main
import (
    "os"
    "fmt"
    "bufio"
)

func abs(val int) int {
    if val < 0 {
        return -1 * val
    }
    return val
}

func main(){
    // your code goes here
    reader := bufio.NewReader(os.Stdin)
    
    var f1, p1, f2, p2 int
    fmt.Fscanln(reader,&f1, &p1, &f2, &p2)
    if abs(f1 - p1) == abs(f2 - p2) {
        fmt.Println("Both")
    } else if abs(f1 - p1) > abs(f2 - p2) {
        fmt.Println("Second")
    } else {
        fmt.Println("First")
    }
    
}

Java solution:
import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner sc=new Scanner(System.in);
        int f1=sc.nextInt();
        int p1=sc.nextInt();
        int f2=sc.nextInt();
        int p2=sc.nextInt();
        int a=f1-p1;
        int b=f2-p2;
        if(Math.abs(a)<Math.abs(b)){
            System.out.println("First");
        }else if(Math.abs(a)==Math.abs(b)){
            System.out.println("Both");
        }else
        System.out.println("Second");
    }
}

JavaScript solution:
// your code goes here
let stdInput = "";

process.stdin.on("data", input => stdInput += input);

process.stdin.on("end", () => {
    const [F1, P1, F2, P2] = stdInput.trim().split(" ").map(Number);
    
    const diff1 = Math.abs(F1 - P1);
    const diff2 = Math.abs(F2 - P2);
    
    if (diff1 < diff2) {
        console.log("First");
    } else if (diff2 < diff1){
        console.log("second");
    } else {
        console.log("Both");
    }
});

Python solution:
# cook your dish here
def choose_best_dish(values):
    F1, P1, F2, P2 = values
    diff1, diff2 = abs(F1 - P1), abs(F2 - P2)
    
    return "First" if diff1 < diff2 else "Second" if diff2 < diff1 else "Both"

values = list(map(int, input().split()))

print(choose_best_dish(values))

Ruby solution:
# Read input and split into integers
input = gets.to_s.split.map(&:to_i)

# Extract the four values
f1, p1, f2, p2 = input[0], input[1], input[2], input[3]

# Calculate absolute differences
diff1 = (f1 - p1).abs
diff2 = (f2 - p2).abs

# Determine which dish to choose
if diff1 < diff2
  puts "First"
elsif diff2 < diff1
  puts "Second"
else
  puts "Both"
end