The base-case is this:
How do you know that we should take three coins at a time ?
The approach :
- First find the
base-case
.
Here the base-case would be to find the maximum number of coins from which you can find the counterfeit coins in just one-weighing
. You can either take two or three coins from which you can find the counterfeit one. So, maximum(two, three) = three.
So, the base-case for this approach would be dividing the available coins by taking three at a time.
2. The generalized formula is 3^n - 3 = (X*2)
where X is the available number of coins and n is the number of weighing's required. (Remember n
should be floored not ceiled).
Consider X = 9 balls. 3^n = 21 and n is ceiled to 2
.
So, the algorithm to tell the minimum number of weighing's would something be similar to:
algo_Min_Weight[int num_Balls]
{
return log base 3 ([num_Balls * 2] + 3);
}