We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
If two piles have the same size, they can be disregarded: a player can keep his or her opponent in a "losing" state by taking as many stones from one pile as said opponent does the other.
The basic "losing" state is having zero piles in front of you. A derivative "losing" state is having two piles of 3 stones [3,3] for the reason described above.
If you can pair off every pile but one (say, two piles of 1 stone, one pile of 2 stones, and two piles of 3 stones [1,1,2,3,3]), you have won (as you can take the odd pile out and then force your opponent to lose).
No matter what you do, if you have a pile of 1 stone, a pile of 2 stones, and a pile of 3 stones in front of you [1,2,3], you've lost. This example is not as arbitrary as it sounds.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Introduction to Nim Game
You are viewing a single comment's thread. Return to all comments →
A few hints for those having trouble.
If two piles have the same size, they can be disregarded: a player can keep his or her opponent in a "losing" state by taking as many stones from one pile as said opponent does the other.
The basic "losing" state is having zero piles in front of you. A derivative "losing" state is having two piles of 3 stones [3,3] for the reason described above.
If you can pair off every pile but one (say, two piles of 1 stone, one pile of 2 stones, and two piles of 3 stones [1,1,2,3,3]), you have won (as you can take the odd pile out and then force your opponent to lose).
No matter what you do, if you have a pile of 1 stone, a pile of 2 stones, and a pile of 3 stones in front of you [1,2,3], you've lost. This example is not as arbitrary as it sounds.