- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- 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

Suppose Amal and Bimal are playing a game. They have a number n and they check whether it is a power of 2 or not. If it is, they divide it by 2. otherwise, they reduce it by the next lower number which is also a power of 2. Whoever reduces the number to 1 will win the game. Amal always starts the game, then we have to find the winner's name.

So, if the input is like n = 19, then the output will be Amal because, 19 is not power of 2, so Amal reduces it to 16, then Bimal divides by 2 to make 8, then again Amal divides by 2 to get 4, then Bimal make it 2 and finally Amal divides to make it 1 and wins the game.

To solve this, we will follow these steps −

- res := 0
- while n > 1, do
- b := 1
- while b * 2 < n, do
- b := b * 2

- n := n - b
- res := res + 1

- if res mod 2 is same as 0, then
- return 'Amal'

- otherwise,
- return 'Bmal'

Let us see the following implementation to get better understanding −

def solve(n): res = 0 while(n > 1): b = 1 while(b * 2 < n): b *= 2 n -= b res += 1 if res % 2 == 0: return 'Amal' else: return 'Bmal' n = 19 print(solve(n))

19

Amal

- Related Questions & Answers
- Program to find winner of a rower reducing game in Python
- Program to find winner of stone game in Python
- Program to find winner of array removal game in Python
- Program to find winner of a rower breaking game in Python
- Program to find the winner of an array game using Python
- Python program to find score and name of winner of minion game
- Program to find winner of a set element removal game in Python
- Program to find number of moves to win deleting repeated integer game in Python
- Find the winner of a game where scores are given as a binary string in Python
- Predict the winner in Coin Game in C++
- Program to find maximum score of brick removal game in Python
- Program to find number of expected moves required to win Lotus and Caterpillar game in Python
- Program to find maximum score in stone game in Python
- Program to find number of possible moves to start the game to win by the starter in Python
- Program to find out if we win in a game in Python

Advertisements