Wednesday, June 29, 2005

Least number of bags - CAT kinda question

This one is a most common one.

Question:
----------
Jaya has 110 one rupee coins with him. He wants to distribute them into bags of various amounts such that he would be able to hand over a whole number of bags for any requested amount of money, which is going to be a whole number from 1 to 110, without adjusting the number of coins in any of the bags. What is the least number of bags required to do so.

Solution:
---------
An amount of 1 rupee to be given away there needs to be a 1 Re bag. So lets have it. So far #of bags is 1. With the existing bags can we give away an amount of Rs.2? No. Either we need to have one more bag of Re.1 or a bag of Re.2. For least number of bags, second choice should be followed. So we will have as the second bag a Re.2 bag. With these existing lets see what all amounts we can give. We can give upto Rs.3 . Now we will make a bag of Re.4 so that we can cater to all amounts till Rs.7. Then again a bag at Re.8, Re.16, Re.32. So far we have bagged (1+2+4+8+16+32 = 63) rupees into bags. And we are in a position to cater to any requested amount upto Rs.63. Since this is greater than half of the total amount (i.e.110/2 = 55). We can stop continuing the above process and bag the remaining amount (i.e. Re.47) into one bag. This process will yield us the least number of required bags. In this case, it is 7.

As a short cut one can remember that for a total amount of 2^n to [2^(n+1)]-1, we will need at least n+1 bags

0 Comments:

Post a Comment

<< Home