Chef and Fruits | CodeChef Solution

Hello coders, today we are going to solve Chef and fruits CodeChef Solution whose Problem Code is FRUITS.

Chef and Fruits

Task

Today is Chef’s birthday. His mom has surprised him with truly fruity gifts: 2 fruit baskets. The first basket contains N apples, and the second one contains M oranges. Chef likes apples and oranges very much but he likes them equally, and therefore, wants to have the minimum possible difference between the number of apples and oranges he has. To do so, he can purchase 1 apple or 1 orange by paying exactly 1 gold coin (that’s some expensive fruit, eh?). Chef can purchase fruits at most K times (as he has only K gold coins in his pocket) to make the difference the minimum possible.

Our little Chef is busy in celebrating his birthday to the fullest, and therefore, he has handed this job to his best friend — you. Can you help him by finding the minimum possible difference he can achieve between the number of apples and orange he owns?

Input Format

The first line of input contains a single integer T denoting the number of test cases. The first and only line of each test case contains 3 space separated integers — NM and K — denoting the number of apples, number of oranges, and number of gold coins our little Chef has.

Output Format

For each test case, output the minimum possible difference between the number of apples and oranges that Chef can achieve.

Constraints

  • 1 ≤ T ≤ 100
  • 1 ≤ N, M, K ≤ 100

Example

Sample Input

3
3 4 1
5 2 1
3 4 3

Sample Output

0
2
0

Explanation

  • Test 1: Chef will buy 1 apple by paying 1 gold coin and will have equal number of apples and oranges.
  • Test 2: Chef will buy 1 orange by paying 1 gold coin and will have 5 apples and 3 oranges.

Solution – Chef and Fruits | CodeChef Solution

C++

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

int main(){
        int t, n, m, k;
        cin >> t;
        while (t--){
                cin >> n >> m >> k;
                if (m > n){
                        if (n + k >= m)
                                printf("%d\n", 0);
                        else
                                printf("%d\n", m - (n + k));
                }
                else if (n > m){
                        if (m + k >= n)
                                printf("%d\n", 0);
                        else
                                printf("%d\n", n - (m + k));
                }
                else
                        printf("%d\n", 0);
        }
        return 0;
}

Python

# cook your dish here
t = int(input())

for i in range(t):
    s = input().split()
    n, m, k = list(map(int, s))
    for i in range(k):
        if n == m:
            break
        elif n > m:
            m += 1 
        else:
            n += 1
    if n > m:
        ans = n - m 
        print(ans)
    else:
        ans = m - n
        print(ans)
        

Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
 Scanner sc = new Scanner(System.in);
        int tc = sc.nextInt();
        while (tc-- > 0) {
            int m = sc.nextInt();
           int n=sc.nextInt();
          int k=sc.nextInt();
          int d=Math.abs(m-n);
          if(k>=d)
              System.out.println(0);
          else
              System.out.println(d-k);
        }
	}
}

Disclaimer: The above Problem (Chef and Fruits) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *