Chef and Candies CodeChef Solution

Problem – Chef and Candies CodeChef Solution

There are N children and Chef wants to give them 1 candy each. Chef already has X candies with him. To buy the rest, he visits a candy shop. In the shop, packets containing exactly 4 candies are available.

Determine the minimum number of candy packets Chef must buy so that he is able to give 1 candy to each of the N children.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • The first and only line of each test case contains two integers N and X — the number of children and the number of candies Chef already has.

Output Format

For each test case, output the minimum number of candy packets Chef must buy so that he is able to give 1 candy to each of the N children.

Constraints

  • 1≤T≤1000
  • 1≤N,X≤100

Sample 1:

Input: 4
20 12
10 100
10 9
20 9
Output: 2
0
1
3

Explanation:

Test Case 1: Chef must buy 2 more packets after which he will have 20 candies which will be enough to distribute 1 candy to each of the 20 children.

Test Case 2: Chef does not need to buy more packets since he already has 100 candies which are enough to distribute 1 candy to each of the 10 children.

Test Case 3: Chef must buy 1 more packet after which he will have 13 candies which will be enough to distribute 1 candy to each of the 10 children.

Test Case 4: Chef must buy 3 more packets after which he will have 21 candies which will be enough to distribute 1 candy to each of the 20 children.

Chef and Candies CodeChef Solution in C++17

#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--)
    {
        int n,x;
        cin>>n>>x;
        n-=x;
        if(n<0)
            n=0;
            
        int ans=n/4;
        
        if(n%4!=0)
            ans++;
        cout<<ans<<endl;
    }
}

Chef and Candies CodeChef Solution in Pyth 3

# cook your dish here
for _ in range(int(input())):
    n,x=map(int,input().split())
    if n<x or n==x:
        print(0)
    elif (n>x) and (n-x)%4 ==0 :
        print((n-x)//4)
    else:
        print(((n-x)//4)+1)

Chef and Candies CodeChef Solution in 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 T=sc.nextInt();
		for(int i=0;i<T;i++){
		    int n=sc.nextInt();
		    int x=sc.nextInt();
		
		    int y=(n-x)/4;
		    if(x>=n){
		        System.out.println("0");
		    }
		    else{
		        n=n-x;
		        if(n%4==0){
		            System.out.println(n/4);
		        }else{
		            System.out.println((n/4)+1);
		        }
		    }
		}
	}
}
Chef and Candies CodeChef Solution Review:

In our experience, we suggest you solve this Chef and Candies CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Chef and Candies CodeChef Solution

Find on CodeChef

Conclusion:

I hope this Chef and Candies CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.