Given 10 stacks of 10 coins each. One of these stacks of coins contains only counterfeit coins, the other stacks do not. A genuine coin has a weight of 10 grams. The weight of a fake coin is 11 grams. You have a contemporary scale with a precise reading. How many balances are required to determine whether the stack is fake? What if you had eleven stacks?
Answer:
Naive Approach: The first solution that hits the mind, is to check one coin from each stack and measure the weight in a balance. In this way, in the worst case, we have to weigh the coins 10 times.
Efficient Solution:
Step 1: Instead of weighing one element from each stack at a time, we can take one coin from each stack and break it into 2 groups of size 5 each (1 group with coins from 1st to 5th stack and the other coins form the other group).
Step 2: If all weigh the same, then the weight of the group will be 5*10 = 50 otherwise, 4*10 + 11 = 51. Now when any 5 is weighed, there can be 2 cases:
- The weight of the group is 50. So the fake coin is on the other group. Otherwise, this group contains the fake coin. Perform the following operations for any of the groups to get the fake coin.
- Divide this group into two parts of 2 coins [from the first 2 stacks of this group] and 3 coins [have the other coins].
Step 3: Now weigh the group of 2 coins.
- If the weight is 20, the fake coin is in the group of 3 coins. Otherwise, the fake coin is in the group of two coins which can be determined by weighing any one of them.
- Divide that group into 2 parts of 2 coins and 1 coin.
Step 4: Now weigh the two coins.
- If this weighs more than 20 then it has the fake coin. Otherwise, the single coin is the fake one.
- If the fake coin is among the 2 coins, then again divide it into two parts
Step 5: To decide the fake one between two coins, weigh any of them.
- If that coin weighs 10, then the other one is the fake.
- Otherwise, the coin which is weighed is the fake coin.
From the above approach, we can see, that we need to weigh at most 4 times in the worst case to find the fake coin.
Optimal Solution: Here we can use the concept of mathematics and the sum of the first N natural numbers.
What we can do is, we can select 1 coin from the 1st stack, 2 coins from the 2nd stack, 3 coins from the 3rd stack, and so on. In this way, we will have total of 55 coins. If all the coins were real, the weight would be 55*10 = 550.
Now consider, that the Kth stack has fake coins.
So the reading of the scale will be:
11*K + (55 – K)*10 [because we have picked K coins from Kth stack].
= 11*K + 550 – 10*K = K + 550.
So the amount of extra weight will be the same as the stack number. Therefore, we can get the stack with only a single balance.
How to check the same if there are 11 stacks of 10 coins?
We can continue the same process as the optimal one. The extra pile is simply treated as pile 0. Then the earlier stated rule still applies (0 coins from 0th stack, 1 coin from the 1st stack, and so on) and the stack number is determined by subtracting the expected weight from the actual weight. The sole modification is that we now consider the probability of the expected weight and observed weight to be equal, which indicates pile 0.