Suppose Amal and Bimal are playing a game. They have n containers with one or more chocolates inside it. These containers are numbered from 1 to N, where ith container has count[i] number of chocolates. Now the game is like. First player will select a container and take one or more chocolates from it. Then the second player will select a non-empty container and take one or more chocolates from it, like this they play alternatively. When one of the players has no way to take any chocolates, then he/she loses the game. If Amal's turn is first we have to find in how many ways Amal can make the first move, such that he always wins.
So, if the input is like count = [2, 3], then the output will be 1, because initially containers are like [2, 3]. They can play like this
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
def solve(count): tmp = 0 for c in count: tmp ^= c if not tmp: return 0 else: moves = 0 for c in count: moves += (tmp^c) < c return moves count = [2, 3] print(solve(count))