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.
/* Finds the winner of the game. Returns true if "First" wins */privatestaticbooleanisFirstPlayerWinner(int[]stones){/* In a single pile, if more than one stones exist then first player will * always win by leaving the last stone for second player to pick up */if(stones.length==1){returnstones[0]>1;}inttotalStones=stones[0];intxorValue=stones[0];for(ints=1;s<stones.length;s++){totalStones+=stones[s];xorValue^=stones[s];}/* If sum of all stones equals the total piles, all piles have a single (1) * stone. For even number of piles, first player will always win. */if(totalStones==stones.length){returntotalStones%2==0;}/* For all other cases, the xor value determines winner. If xor value = 0, * then second player will always win as all piles (stones) can be paired. */returnxorValue>0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Misère Nim
You are viewing a single comment's thread. Return to all comments →
My Java solution :