Cadbury Problem
Cadbury Problem
In DPS School, Cadbury bars have to be distributed to children waiting in a queue. Each
Cadbury bar is rectangular in shape. Consider its side lengths are integer values.
The distribution procedure is as follows:1. If bar is not square in shape, then the largest possible square piece of Cadbury is
broken and given to the first child in queue.
2. If bar is square in shape, then complete bar is given to the first child in queue.
Once a child receives his share of Cadbury, he leaves the queue. The remaining portion
of the Cadbury bar is dealt in same fashion and the whole or a portion of it is given to the
next child in the queue.
School has got a carton of Cadbury bars to be distributed among the children all over the
School. The Cadbury bars in the carton are of different sizes. A bar of length i and
breadth j is considered to be different from a bar of length j and breadth i.
For every i such that M<=i<=N and every j such that P<=j<=Q (where M, N, P and Q are
integers). Each Cadbury bar in carton is unique in length (i) and breath(j).
Given the values of M, N, P and Q (where M, N values are the ranges for length of
Cadbury and P, Q values are the ranges for breadth of Cadbury). Find the number of
children who will receive Cadbury from the carton.
Input Specification:
M, N, P, Q are of integer type (M, N values are the ranges for length of Cadbury bar. P, Q
values are the ranges for breadth of Cadbury bar).
Output Specification:
Number of children who will receive Cadbury bar from the carton.
M = 5, N = 6, P = 3, Q=4
Here, i can be from 5 to 6 and j can be from 3 to 4. So the four bars will be in carton of
sizes 5x3, 5x4, 6x3, 6x4.
First we choose a cadbury bar of size 5x3
first child would receive 3x3 portion ( remaining 2x3 portion )
next child would receive 2x2 portion ( remaining 2x1 portion )
now the remaining portion are 2 square pieces of (1x1), which can be given to 2 more
children
So the Cadbury bar with the size of 5x3 can be distributed to 4 children.
Similarly we can find out number of children for rest of the combinations (i.e. 5x4, 6x3,
6x4) in the given range as follows
to 4 children.
to 5 children.
to 2 children.
to 3 children.
So the whole carton can be distributed among 14 children. Hence the output will be 14.
Instructions:
1) Do not write main function.
2) You can print and debug your code at any step of the code.
3) You need to return the required output from the given function.
4) Do not change the function and parameter names given in editor code.
5) Return type must be the same as mentioned in the problem statement.
6) When you submit your code, 10 test cases of different complexity level are executed
in the background and marks are given based on number of test cases passed.
7) If you do not plan to complete the code in one sitting, then please save your work on a
local machine. The code is saved only when it has been submitted using Submit button.
8 ) Only one submission is allowed.
Crazy crow
There are N pots. Every pots have some water in it. They may be partially filled. So there is a
Overflow Number O associated with every pot which tell how many minimum stone pieces are
require for that pot to overflow. So if for a pot O-value is 5 it means minimum 5 stone pieces
should be put in that pot to make it overflow. Initially a crow watched those pots and by seeing
the water level he anticipated O-value correctly for every pot ( that is he knew O1 to On). But
when he came back in evening he found that every pot is painted from outside and he is not able
to know which pot has what O-value. Crow wants some K pots to overflow so that he can serve
his child appropriately. For overflow of pots he need to search for stone in forest( assume that
every stone has same size). He wants to use minimum number of stones required to overflow K
pots. But only he know the O-value of pots he doesn't know now which pot has what O-value. So
the task is that in what minimum number of stones he can make K pots overflow in worst case.
Input Specification:1) A array O corresponding to O-value of N pots {O1, O2, ....... , On}2)
Number of pots3) K -value ( number of pots which the crow wants to overflow}
Output Specification:Minimum number of stones required to make K pots overflow in worst
case.or-1 if input is invalid
Example:
Let say there are two pots
pot 1 has O value of 5 , O1= 5pot 2 has O value of 58, O2= 58
Let say crow wants to make one of the pot to overflow. If he know which pot has what O-value
he would simple search for 5 stones and put then in pot 1 to make it overflow. But in real case he
doesn't know which pot has what O-value so just 5 stones may not always work. However he
does know that one pot has O-value 5 and other has 58. So even in worst case he can make one
of the pot overflow just by using 10 stones. He would put 5 stones in one pot if it doesn't overflow
he would try the remaining 5 in the other pot which would definitely overflow because one of the
pot has O-value of 5.So the answer for above question is minimum 10 stones even in worst
case.
Input : Input 1= {5,58}Input 2= 2
Input 3= 1 Output : 10
Instructions: 1) Do not write main function. 2) You can print and debug your code at any step of
the code.3) You need to return the required output from the given function. 4) Do not change the
function and parameter names given in editor code.5) Return type must be the same as
mentioned in the problem statement. 6) When you submit your code, 10 test cases of different
complexity level are executed in the background and marks are given based on number of test
cases passed. 7) If you do not plan to complete the code in one sitting, then please save your
work on a local machine. The code is saved only when it has been submitted using Submit
button. 8 ) Only one submission are allowed.