Find winner in game of N balls, in which a player can remove any balls in range [A, B] in a single move
Last Updated :
02 Jul, 2021
Given two integers A and B, and also given that Alice and Bob are playing a game starting with a bag containing N balls, in which, in a single move, a player can remove any number of balls between the range [A, B] and if the player cannot remove any balls, then the player loses, the task is to find the winner of the game if Alice and Bob play the game alternatively and optimally and Alice starts the game.
Examples:
Input: N = 2, A = 1, B = 1
Output: Bob
Explanation:
One way in which the game can be played is:
- Alice removes 1 ball, so the remaining balls are 1.
- Now, Bob removes the last ball.
- Alice cannot remove any balls, so she loses.
Input: N = 3, A = 1, B = 2
Output: Bob
Naive Approach: The simplest approach is to find the grundy number for every state and find the winning and losing states by using Sprague – Grundy Theorem.
Time Complexity: O(N*N!)
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized based on the following observations:
- Firstly, it can be observed that (A + B) is always losing state, because whatever X (A ? X ? B) number of balls the current player chooses, the opponent can always empty the bag as there will be (A + B – X) number of balls left where A ? (A + B – X) ? B.
- Also, from previous observations, one can observe that, for any multiple of (A + B) say, m*(A + B), the opponent can always reduce the current player to a state of (m – 1)*(A + B) and from (m – 1)*(A + B) to (m – 2)*(A + B) and so on.
- Thus, extending the above observation, one can say that for any multiple of (A + B), the opponent can always reduce the current player to a state of exactly (A + B), which is indeed a losing state. Therefore, all multiples of (A + B) are a losing state.
- So, the optimal choice for any player is to reduce the opponent to a multiple of (A + B), because after this, the player can always win, no matter what the opponent’s moves are.
- So, now losing states are the states from which one can never reduce the opponent to a multiple of (A + B).
- Therefore, any player with the state of the form: ((A + B)*m + y), where (0 ? y ? A-1) can never force the opponent to reduce to a multiple of (A + B), as any player can only pick at least A and at most B number of balls.
Follow the steps below to solve the problem:
- If N%(A+B) is less than A, then print “Bob“.
- Otherwise, print “Alice“.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string NimGame( int N, int A, int B)
{
int sum = A + B;
if (N % sum <= A - 1)
return "Bob" ;
else
return "Alice" ;
}
int main()
{
int N = 3, A = 1, B = 2;
cout << NimGame(N, A, B) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static String NimGame( int N, int A, int B)
{
int sum = A + B;
if (N % sum <= A - 1 )
return "Bob" ;
else
return "Alice" ;
}
public static void main (String[] args)
{
int N = 3 , A = 1 , B = 2 ;
System.out.println(NimGame(N, A, B));
}
}
|
Python3
def NimGame(N, A, B):
sum = A + B
if (N % sum < = A - 1 ):
return "Bob"
else :
return "Alice"
N = 3
A = 1
B = 2
print (NimGame(N, A, B))
|
C#
using System;
class GFG{
public static String NimGame( int N, int A, int B)
{
int sum = A + B;
if (N % sum <= A - 1)
return "Bob" ;
else
return "Alice" ;
}
static void Main()
{
int N = 3, A = 1, B = 2;
Console.Write(NimGame(N, A, B));
}
}
|
Javascript
<script>
function NimGame(N, A, B) {
let sum = A + B;
if (N % sum <= A - 1)
return "Bob" ;
else
return "Alice" ;
}
let N = 3, A = 1, B = 2;
document.write(NimGame(N, A, B) + "<br>" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...