Algorithm


  problem Link :  https://www.codechef.com/problems/CHEFCAND 

Problem

There are  children and Chef wants to give them 1 candy each. Chef already has  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  children.

Input Format

  • The first line of input will contain a single integer , denoting the number of test cases.
  • The first and only line of each test case contains two integers  and  — 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  children.

Constraints

  • 1≤�≤1000
  • 1≤�,�≤100

Sample 1:

Input
 
Output
 
4
20 12
10 100
10 9
20 9
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.

Code Examples

#1 Code Example with C Programming

Code - C Programming

#include<stdio.h>
int main()
{
    int t,n,x,p,q;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&x);
        p=(n-x)/4;
        q=(n-x)%4;
        if(n<=x) printf("0\n">;
        else if(n>x && q==0) printf ("%d\n",p);
        else printf("%d\n",p+1);
    }
} .
Copy The Code & Try With Live Editor

Input

x
+
cmd
4
20 12
10 100
10 9
20 9

Output

x
+
cmd
2
0,br> 1
3
Advertisements

Demonstration


CodeChef solution CHEFCAND  - Chef and Candies Codechef solution in C,C++

Previous
CodeChef solution SUGERCANE Sugarcane Juice Business CodeChef solution in C,C++
Next
CodeChef solution TRUESCORE - Is the Score Consistent Codechef solution in C,C++