Algorithm
problem Link : https://www.codechef.com/problems/CHEFCAND
Problem
There are children and Chef wants to give them candy each. Chef already has candies with him. To buy the rest, he visits a candy shop. In the shop, packets containing exactly candies are available.
Determine the minimum number of candy packets Chef must buy so that he is able to give 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 candy to each of the children.
Constraints
Sample 1:
4 20 12 10 100 10 9 20 9
2 0 1 3
Explanation:
Test Case : Chef must buy more packets after which he will have candies which will be enough to distribute candy to each of the children.
Test Case : Chef does not need to buy more packets since he already has candies which are enough to distribute candy to each of the children.
Test Case : Chef must buy more packet after which he will have candies which will be enough to distribute candy to each of the children.
Test Case : Chef must buy more packets after which he will have candies which will be enough to distribute candy to each of the 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
20 12
10 100
10 9
20 9
Output
0,br> 1
3
Demonstration
CodeChef solution CHEFCAND - Chef and Candies Codechef solution in C,C++