Type Here to Get Search Results !

Dole Out Cadbury | TCS MockVIta 2 2020 | By CodingHumans |

2

Dole Out Cadbury


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

Explanation

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.


Recommended: Please try your approach on your integrated development environment (IDE) first, before moving on to the solution.

Few words from CodingHumans : Don't Just copy paste the solution, try to analyze the problem and solve it without looking by taking the the solution as a hint or a reference . Your understanding of the solution matters.


HAPPY CODING 😁




Solution:
( 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;
}


Solution:
( 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 . 

Post a Comment

2 Comments
  1. sorry, output for above code id showing incorrect
    not even showing test case output

    ReplyDelete
    Replies
    1. can you please tell us which programming language you are talking about so that we can have a look to your issue

      Delete
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad