Você está na página 1de 1

1) Brute Force Algorithm: Iterate through all the stacks one by one(1 to n) and measure weight

of one coin from each. The coin which weights 11gm, that stack is counterfeit.

Algorithm:

for i = 1 to n:

if( weight of a coin(top of stack) of stack(i) == 11gm) print(stack(i) is counterfeit)

Complexity of Brute Force algorithm: O(n) In worst case the last stack we check might be
counterfeit, hence our loop can run max of n times.

2) There is only one weighing needed to identify the stack with the fake coin.

Number the coin stacks from 1 to n. Take 1 coin from the first stack, 2 coins from the second,
and so on, until all n coins are taken from the last stack.

Weigh all these coins together. The difference between this weight and (n*(n+1)/2)*10, the
weight of (1 + 2 + ... + n) = (n*(n+1)/2) genuine coins, indicates the number of the fake coins
weighted, which is equal to the number of the stack with the fake coins.

For example, if the selected coins weigh ((n*(n+1)/2)*10 + 3 ) grams, 3 coins are fake and
hence it is the third stack that contains the fake coins.

Você também pode gostar