50 coins are lined up on a table (initially all heads facing upwards). 50 people pass from the coins one by one in the following manner:
The first person comes and visits the coins numbered 1, 2, 3, 4, …. and flips the coins until an opposite face comes(For ex-if it was head initially, he/she flips it until the tail comes and vice versa.)
The second person comes and visits the coins numbered 2, 4, 6, 8….. and flips the coins.
Similarly, the third person comes and visits (3, 6, 9, 12, ……) and does the same.
The task is to find the final states of all the coins after 50th person passes i.e. whether a coin is facing heads or tails upwards?
Solution: The trick is to identify if a coin is being visited by people even or an odd number of times.
Pay attention to the coin number.
Only perfect squares have odd no. of factors, and every other number has even number of factors.
For all those coins which have an odd number of factors will be facing tails upwards and for all those coins which have even no. of factors will be facing heads upwards.
So, coins numbered 1, 4, 9, 16, 25…. will have tails and 2, 3, 5, 6, 7, 10, 11, … will have heads facing upwards.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.