- 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 winner of a rower reducing game in Python

Suppose we have an array height. There are n different towers with different height. Amal and Bimal are playing a game. The game rules are like below

Amal always plays first

During each move, the current player selects a tower of height X and reduce the height to Y [1 <= Y < X; Y evenly divides X]

Whoever has no move will lose the game

We have to find the winner’s name.

So, if the input is like height = [3,1,2], then the output will be Bimal, because the initial heights are {3,1,2}. If Amal reduces the height of tower 2 to 1, Bimal can reduce 3 by 1, but Amal has no move so Bimal wins.

To solve this, we will follow these steps −

- Define a function util() . This will take a,n
- ans := 0
- for i in range 0 to n - 1, do
- ans := ans XOR a[i]

- return ans
- From the main method do the following
- n := size of height
- b := an array of size n and fill with 0
- for i in range 0 to n - 1, do
- if height[i] is same as 1, then
- b[i] := 0

- otherwise
- b[i] := 0
- j := 2
- root := floor of square root of height[i]
- while height[i] is not same as 1 and j<=root, do
- if height[i] mod j is same as 0, then
- while height[i] mod j is same as 0, do
- b[i] := b[i] + 1
- height[i] := floor of height[i]/j

- while height[i] mod j is same as 0, do
- j := j + 1

- if height[i] mod j is same as 0, then
- if height[i] is not same as 1, then
- b[i] := b[i] + 1

- if height[i] is same as 1, then
- ans := util(b, n)
- if ans is not same as 0, then
- return "Amal"

- otherwise,
- return "Bimal"

## Example

Let us see the following implementation to get better understanding −

def util(a,n): ans = 0 for i in range(n): ans = ans^a[i] return ans def solve(height): n = len(height) b = [0 for i in range(n)] for i in range(n): if(height[i] == 1): b[i] = 0 else: b[i] = 0 j = 2 root = int(pow(height[i],0.5)) while(height[i] != 1 and j<=root): if(height[i]%j == 0): while(height[i]%j == 0): b[i] += 1 height[i] = height[i]//j j += 1 if(height[i] != 1): b[i] += 1 ans = util(b, n) if(ans != 0): return "Amal" else: return "Bimal" height = [3,1,2] print(solve(height))

## Input

[3,1,2]

## Output

Bimal

- Related Articles
- Program to find winner of a rower breaking game in Python
- Program to find winner of number 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 set element removal game in Python
- Program to find the winner of an array game using Python
- C++ program to find winner of card game
- Python program to find score and name of winner of minion game
- C++ program to find winner of cell coloring game
- C++ program to find winner of ball removal game
- C++ Program to find winner of unique bidding game
- C++ Program to find winner name of stick crossing game
- C++ program to find winner of typing game after delay timing
- Find the winner of a game where scores are given as a binary string in Python
- Predict the winner in Coin Game in C++

Advertisements