# 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
• j := j + 1
• if height[i] is not same as 1, then
• b[i] := b[i] + 1
• 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