![]() |
VOOZH | about |
Given an integer N and two players, A and B are playing a game. On each player’s turn, that player makes a move by subtracting a divisor of current N (which is less than N) from current N, thus forming a new N for the next turn. The player who does not have any divisor left to subtract loses the game. The task is to tell which player wins the game if player A takes the first turn, assuming both players play optimally.
Examples:
Input : N = 2
Output : Player A wins
Explanation :
Player A chooses 1, and B has no more moves.
Input : N = 3
Output : Player B wins
Explanation :
Player A chooses 1, player B chooses 1, and A has no more moves.
Approach :
This problem mentioned above can be solved using Dynamic Programming.
N -> current number left
A -> boolean value to decide if it's player A's turn or not
Player B wins
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)