Nim Game Problem (Game is Love) with Solution
Let's say two players (p1, p2) are playing a game, in which they have two piles
of stones N1 and N2. They take a turns by picking any even positive number of
piles and keep half of them on other piles and throw another half. Whoever can
not make a move loses. p1 makes the first move. Consider that both players will
play optimized.
(Level: Hard)
INPUT: N1 = 3 and N2 = 6
OUTPUT: p1 wins just (just print--> p1)
EXPLANATION:
3 6 --- This is the case
4 4 --- move made by p1
2 5 --- move made by p2
3 3 --- move made by p1
1 4 --- move made by p2
2 2 --- move made by p1
1 3 --- move made by p2
2 1 --- move made by p1
0 2 --- move made by p2
1 0 --- move made by p1
Now as you can see, there is nothing to move (for p2) so that
p1 win's the game.
Here is the solution in python :
Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment.
To solve more problems on Array and String, please check out these articles "Top 10 Array Interview Questions" and "Top 10 String Interview Questions".
All the Best!
Comments
Post a Comment