This one is for solving problem , math puzzle and fun

General Rules

  • This Forum Rules:
    1. No asking for course requests or download links.
    2. Don't share links to other websites for downloads or references.
    3. Avoid controversial topics in discussions.

    4. Use an appropriate thread title that matches your content, not just a word.

    Other Forum Rules Can Be Found Here

Yahia Bakleh

New member
TutFlixer
Feb 9, 2022
22
18
3
Syria
Problem: (from book cracking the code interview : 6.1 The Heavy Pill )
You have 20 bottles of pills. 19 bottles have 1.0 gram pills, but one has pills of weight 1.1 grams. Given a scale that provides an exact measurement, how would you find the heavy bottle? You can only use the scale once

Solution:
Because we can only use the scale once, we know something interesting: we must weigh multiple pills at the same time. In fact, we know we must weigh pills from at least 19 bottles at the same time. Otherwise, if we skipped two or more bottles entirely, how could we distinguish between those missed bottles? Remember that we only have one chance to use the scale.

So how can we weigh pills from more than one bottle and discover which bottle has the heavy pills? Let's suppose there were just two bottles, one of which had heavier pills. If we took one pill from each bottle, we would get a weight of 2.1 grams, but we wouldn't know which bottle contributed the extra 0.1 grams. We know we must treat the bottles differently somehow

If we took one pill from Bottle #1 and two pills from Bottle #2, what would the scale show? It depends. If Bottle #1 were the heavy bottle, we would get 3.1 grams. If Bottle #2 were the heavy bottle, we would get 3.2 grams. And that is the trick to this problem.

we know the "expected" weight of a bunch of pills. The difference between the expected weight and the actual weight will indicate which bottle contributed the heavier pills, provided we select a different number of pills from each bottle.

we can generalize this to the full solution: take one pill from Bottle #1, two pills from Bottle #2, three pills from Bottle #3, and so on. Weigh this mix of pills. If all pills were one gram each, the scale would read 210 grams
(1 + 2 + • • • + 20 = 20 * 21 / 2 = 210). Any "overage" must come from the extra 0.1 gram pills.

This formula will tell you the bottle number:
(weight - 210grams)/0.1grams

So, if the set of pills weighed 211.3 grams, then Bottle #13 would have the heavy pills.
 

gr33dy

New member
TutFlixer
Oct 15, 2020
5
0
1
Pakistan
For anyone to whom it might look a little difficult to understand.

Basically think it this way,

First -> What can be the total weight of pills from each bother when taken according to above mentioned method (considering all pills are 1.0 grams? That will be 210 as you can see above.

Next -> We use the weighing scale only once to get the actual weight of the pills that we have. For Eg:
(1+2+3+4+5+6+7+8+9+10+12.1+12+13+14+15+16+17+18+19+20) (considering pills from bottle 11 weight 1.1 grams)
Note: It is necessary for the weight to be over 210 because at least one of the 20 bottles has pills with weight more then 1.0 grams as mentioned in question)
The total of the weighing scale in our case = 211.1

Next -> Now we have a value for the minimum possible weight of pills if all were 1 grams (210) and the actual value of pills that we got from our weighing (211.1)

Now we find the difference between the 2 to see how much more the weight is (1.1 grams in our case 211.1-210)

After finding the difference, we divide it by 0.1 (this is the excess of weight of one of the pill bottles we have),

By dividing the value of difference from 2 weights with 0.1 will give us the number of additional weight which is (1.1 / 0.1 = 11) also our selected number.

It can also be seen is:
1 = 0.1
x = 1.1
and then simply find value by cross multiplication
 

Latest resources