Chef and Candies Solution Codechef

Chef and Candies Solution Codechef

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 Input 1

4
20 12
10 100
10 9
20 9

Sample Output 1

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.

SOLUTION

Program: Chef and Candies Solution in C++

#include <iostream>
using namespace std;
int main() {
	int t;
	cin>>t;
	while(t>0)
	{
	    int n,x;
	    cin>>n>>x;
	    if(x>=n)
	    cout<<0<<endl;
	    else
	    {
	        int k=n-x;
	        if(k%4==0)
	        cout<<k/4<<endl;
	        else
	        cout<<k/4 +1<<endl;
	    }
	    t--;
	}
	return 0;
}

Program: Chef and Candies Solution in Python

for _ in range(int(input())):
    n, x = [int(x) for x in input().split()]
    remaining = n-x
    if remaining < 0:
        print(0)
    else:
        count = 0
        while remaining > 0:
            remaining -= 4
            count += 1
        print(count)

Program: Chef and Candies Solution in Java

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 n = Sc.nextInt();
		for(int i = 0; i < n; i++)
		{
		    int N = Sc.nextInt();
		    int X = Sc.nextInt();
		    if(X >= N)
		    {
		        System.out.println(0);
		    }
		    else
		    {
		        int j = 0;
		        while(4*j < (N-X))
		        {
		            j++;
		        }
		        System.out.println(j);
		    }
		}
	}
}

Related:

Leave a Comment

15 − eight =