- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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
- Related Articles
- Program to find number of moves to win deleting repeated integer game in Python
- Program to find number of expected moves required to win Lotus and Caterpillar game in Python
- Program to find out if we win in a game in Python
- Program to find out the minimum moves in a snakes and ladders game in Python
- C++ program to find smallest and largest number of children in game before start
- Program to check person 1 can win the candy game by taking maximum score or not in Python
- Program to find winner of number reducing game in Python
- Program to Find Out the Number of Moves to Reach the Finish Line in Python
- Program to check whether Amal can win stone game or not in Python
- Minimum Players required to win the game in C++
- C++ program to find maximum possible amount of allowance after playing the game
- Program to find the number of possible position in a line in Python
- Program to check whether first player win in candy remove game or not in Python?
- Program to find possible number of palindromes we can make by trimming string in Python
- Program to find winner of stone game in Python
