This post explains the TCS CodeVita problem Dole Out Cadbury step by step: the problem statement, input and output format, constraints, a sample test case and a working solution in C and Python that you can study and run.

Problem Description
You are a teacher in reputed school. During Celebration Day you were assigned a task to distribute Cadbury such that maximum children get the chocolate. You have a box full of Cadbury with different width and height. You can only distribute largest square shape Cadbury. So if you have a Cadbury of length 10 and width 5, then you need to break Cadbury in 5X5 square and distribute to first child and then remaining 5X5 to next in queue
Constraints
0<P<Q<1501
0<R<S<1501
Input Format
First line contains an integer P that denotes minimum length of Cadbury in the box
Second line contains an integer Q that denotes maximum length of Cadbury in the box
Third line contains an integer R that denotes minimum width of Cadbury in the box
Fourth line contains an integer S that denotes maximum width of Cadbury in the box
Output
Print total number of children who will get chocolate.
Timeout
1
Example 1
Input
5
7
3
4
Output
24
Explanation
Length is in between 5 to 7 and width is in between 3 to 4.
So we have 5X3,5X4,6X3,6X4,7X3,7X4 type of Cadbury in the box.
If we take 5X3 :
First, we can give 3X3 square Cadbury to 1st child .Then we are left with 3X2. Now largest square is 2X2 which will be given to next child. Next, we are left with two 1X1 part of Cadbury which will be given to another two children.
And so on.
Dole Out Cadbury Solution in C
#include<stdio.h>
int no_of_children(int row, int col)
{
int count=0;
int total = rw * col;
while(row && co)
count++;
if(row>col)
row = row - col;
else
col = col - row;
}
return count;
}
int main()
{
int sum=0;
int minlen,maxlen,minwid,maxwid;
scanf("%d\n%d\n%d\n%d",&minlen,&maxlen,&minwid,&maxwid);
if(0<minlen<1501 && 0<maxlen<1501 && 0<minwid<1501 && 0<maxwid<1501)
{
for(int i=minlen;i<=maxlen;i++)
{
for(int j=minwid;j<=maxwid;j++)
{
sum = sum + no_of_children(i,j);
}
}
printf("%d",sum);
}
return 0;
}
Dole Out Cadbury Solution in Python
P=int(input())
Q=int(input())
R=int(input())
S=int(input())
total=0
for i in range(P,Q+1):
for j in range(R,S+1):
a=max(i,j)
b=min(i,j)
while b!=1:
if a==b:
a=1
break
else:
d=a-b
a=max(d,b)
b=min(d,b)
total+=1
total+=a
print(total)
Problem was created by TCS MockVita 2 2020.
If you have any doubts regarding this problem or need the solution in other programming languages then leave a comment down below .
Tip: try to solve the problem yourself before reading the solution. Use the code as a reference to check your approach, and make sure you understand why it works.
More TCS CodeVita solutions All coding problems MCQ practice
See everything in one place: Coding Problems and Solutions
sorry, output for above code id showing incorrect
ReplyDeletenot even showing test case output
can you please tell us which programming language you are talking about so that we can have a look to your issue
Delete