Program to find winner of number reducing game in Python

Suppose Amal and Bimal are playing a number reducing game. They have a number n and follow these rules: If n is a power of 2, divide it by 2. Otherwise, reduce it by the next lower number which is also a power of 2. Whoever reduces the number to 1 wins the game. Amal always starts first.

For example, if n = 19: Amal reduces 19 to 16 (19 - 16 = 16, where 16 is the largest power of 2 less than 19), then Bimal divides 16 by 2 to get 8, Amal divides 8 by 2 to get 4, Bimal makes it 2, and finally Amal divides by 2 to make it 1 and wins.

Algorithm

To solve this problem, we follow these steps −

  • Initialize a counter res = 0 to track the number of moves
  • While n > 1:
    • Find the largest power of 2 less than n
    • Subtract this power of 2 from n
    • Increment the move counter
  • If the total moves is even, Amal wins (since he starts first)
  • If the total moves is odd, Bimal wins

Implementation

def solve(n):
    res = 0
    while n > 1:
        # Find the largest power of 2 less than n
        b = 1
        while b * 2 < n:
            b *= 2
        
        # Reduce n by this power of 2
        n -= b
        res += 1
    
    # Amal starts first, so even moves mean Amal wins
    if res % 2 == 0:
        return 'Amal'
    else:
        return 'Bimal'

# Test with the example
n = 19
result = solve(n)
print(f"Winner for n = {n}: {result}")
Winner for n = 19: Bimal

Step-by-Step Trace

Let's trace through the game with n = 19 ?

def solve_with_trace(n):
    res = 0
    original_n = n
    print(f"Starting game with n = {n}")
    
    while n > 1:
        # Find the largest power of 2 less than n
        b = 1
        while b * 2 < n:
            b *= 2
        
        player = "Amal" if res % 2 == 0 else "Bimal"
        print(f"Move {res + 1}: {player} reduces {n} by {b} to get {n - b}")
        
        n -= b
        res += 1
    
    winner = "Amal" if res % 2 == 0 else "Bimal"
    print(f"Game over! Total moves: {res}, Winner: {winner}")
    return winner

solve_with_trace(19)
Starting game with n = 19
Move 1: Amal reduces 19 by 16 to get 3
Move 2: Bimal reduces 3 by 2 to get 1
Game over! Total moves: 2, Winner: Amal

Multiple Test Cases

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

# Test multiple cases
test_cases = [1, 2, 3, 8, 15, 19, 32]

for n in test_cases:
    winner = solve(n)
    print(f"n = {n:2d} ? Winner: {winner}")
n =  1 ? Winner: Amal
n =  2 ? Winner: Bimal
n =  3 ? Winner: Amal
n =  8 ? Winner: Amal
n = 15 ? Winner: Bimal
n = 19 ? Winner: Amal
n = 32 ? Winner: Bimal

Conclusion

The winner is determined by counting the total number of moves required to reduce n to 1. Since Amal starts first, an even number of total moves means Amal wins, while an odd number means Bimal wins.

Updated on: 2026-03-26T18:15:32+05:30

274 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements