There are n glasses on the table, all standing upside down. In one move, you are allowed to turn over exactly n – 1 of them. Determine all values of n for which all the glasses can be turned up in the minimum number of moves.
There exists 2 cases, i.e, when the value of n is odd and when the value of n is even. If the value of n is odd, then there exists no solution.If the value of n is even, the problem can be solved in n moves, which is the minimum number of moves required.
If n is odd : As in one move,we are allowed to turn over exactly n – 1 of them.If n is odd then n – 1 i.e, the number of glasses turned over in one move, is even. Therefore, the parity of the number of glasses that are upside down will always remain odd, as it was in the initial position. Hence, the final position in which the number of upside down glasses is 0, which is even, cannot be reached.
If n is even : Let us assume that the glasses are numbered from 1 to n.If n is even, the problem can be solved by making the following move n times: turn over all the glasses except the ith glass, where i = 1, 2, . . . , n.
Here is an example of the algorithm’s application for n = 6. Let us denote the upside down glasses by 1’s and the others as 0’s. A glass that is not turned over on the next move is shown in bold.
If the value of i is 1, then turn over all the glasses except the 1st glass.
111111 –> 100000
Similarly, If the value of i is 2, then turn over all the glasses except the 2nd glass.
100000 –> 001111
Similarly, If the value of i is 3, then turn over all the glasses except the 3rd glass.
001111 –> 111000 and so on.
So, for n = 6, this will look like :
111111 –> 100000 –> 001111 –> 111000 –> 000011 –> 111110 –> 000000
Since any two consecutive moves will either have no impact on the state of the glasses or change the state of exactly two of them, no algorithm can solve the problem in fewer than n moves.
- Puzzle 1 | (How to Measure 45 minutes using two identical wires?)
- Puzzle 2 | (Find ages of daughters)
- Puzzle 3 | (Calculate total distance travelled by bee)
- Puzzle 4 | (Pay an employee using a 7 units gold rod?)
- Puzzle 13 | (100 Prisoners with Red/Black Hats)
- Puzzle 5 | (Finding the Injection for Anesthesia)
- Puzzle 6 | (Monty Hall problem)
- Puzzle 7 | (3 Bulbs and 3 Switches)
- Puzzle 8 | (Find the Jar with contaminated pills)
- Puzzle 9 | (Find the fastest 3 horses)
- Puzzle 10 | (A Man with Medical Condition and 2 Pills)
- Puzzle 11 | (1000 Coins and 10 Bags)
- Puzzle 12 | (Maximize probability of White Ball)
- Puzzle 14 | (Strategy for a 2 Player Coin Game)
- Puzzle 15 | (Camel and Banana Puzzle)
- Puzzle 17 | (Ratio of Boys and Girls in a Country where people want only boys)
- Puzzle 16 | (100 Doors)
- Puzzle 18 | (Torch and Bridge)
- Puzzle 19 | (Poison and Rat)
- Puzzle 20 | (5 Pirates and 100 Gold Coins)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.