 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find number of possible moves to start the game to win by the starter in Python
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
- Amal picks one chocolate from the second container, so currently [2, 2]
- Bimal picks one chocolate from the first container, so currently [1, 2]
- Amal picks one chocolate from the second container, so currently [1, 1]
- Bimal picks one chocolate from the first container, so currently [0, 1]
To solve this, we will follow these steps −
- tmp := 0
- for each c in count, do- tmp := tmp XOR c
 
- if tmp is zero, then- return 0
 
- otherwise,- moves := 0
- for each c in count, do- moves := moves + (1 when (tmp XOR c) < c, otherwise 0)
 
- return moves
 
Example
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))
Input
[2, 3]
Output
1
