Rohit wants to give each of the N youngsters, 1 candy. Rohit is already carrying M candies. He goes to a candy store to purchase the remainder. There are exactly 4 candy-filled packets available in the shop.
Find the bare minimum of candy packets that Rohit must purchase to be able to distribute 1 candy to each of the N kids.
Explanation:
Test Case 1: Rohit has to purchase 2 more packets before he has enough sweets to provide 1 candy to each of the 20 kids.
Test Case 2: Rohit already has 100 candies, which is enough to provide 1 candy to each of the 10 kids, so he doesn’t need to buy any more packets.
Test Case 3: Rohit has to purchase 1 more packets to obtain 13 candies, which is enough to provide 1 candy to each of the 10 kids.
Test Case 4: The chef needs to purchase 3 more packets until he has 21 candies, which is enough to provide 1 candy to each of the 20 kids.
Input Format:
The first line of input will contain a single integer T, signifying the number of test cases.
The first and individual line of each test case comprises two values N and M — the number of children and the amount of candies Rohit already has.
Sample Input:
4 20 12 10 100 10 9 20 9
Constraints:
1 ≤ T ≤ 1000 1 ≤ N, M ≤ 100
Output Format:
Provide the least quantity of candy packets that Rohit needs to purchase for each test scenario in order to provide 1 candy to each of the N youngsters.
Sample Output:
2 0 1 3
Solution
C++
#include<bits/stdc++.h>
using namespace std;
void myfunc(){
int x,y;
cin>>x>>y;
if(y<x){
int k;
if((x-y)%4==0){
k=(x-y)/4;
}
else{
k=((x-y)/4)+1;
//sauravhathi
}
cout<<k<<endl;
}
else{
cout<<0<<endl;
}
}
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
myfunc();
}
}
Python
import math
t = int(input())
for i in range(t):
inp = [int(i) for i in input().strip().split()]
dif = inp[0] - inp[1]
if dif > 0:
#sauravhathi
print(math.ceil(dif/4))
if dif <= 0:
print(0)
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class Sauravhathi
{
public static int findMinCandy(Double n, Double x) {
if (x >= n)
return 0;
else {
//sauravhathi
return (int) Math.ceil((n - x) / 4);
}
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while (t-- > 0) {
Double n = scanner.nextDouble();
Double x = scanner.nextDouble();
System.out.println(findMinCandy(n, x));
}
scanner.close();
}
}
C
#include<stdio.h>
int main()
{
int test; scanf("%d",&test);
while(test--)
{
int x;
scanf("%d",&x);
int y;
scanf("%d",&y);
int main;
main=x-y;
if(main<=0){
printf("0\n");
}
else if(main%4!=0){
int z=(main/4)+1;
//sauravhathi
printf("%d\n",z);
}
else if(main%4==0){
int w=(main/4);
printf("%d\n",w);
}
}
return 0;
}
Happy Learning – If you require any further information, feel free to contact me.