Open in App
Not now

Find the winner of the game based on the given moves

• Last Updated : 08 Mar, 2023

Given two integers N and M. Two players A and B are playing this game, and the task is to find the winner A or B according to the given moves if both players play the game optimally and A starts first. The move of the games are as follows:

• The player destroys anyone from both integers and then divides the other integer into two new integers.
• The game is playable until any player is not able to make any moves

Examples:

Input: N = 2, M = 1
Output: A
Explanation: Move 1: Player A destroys the integer M = 1 and splits the integer N = 2 into two new integers 1 and 1 let say them updated values in form of N and M, which are N = 1 and M = 1 .Now Player B can destroy any of the integer either N or M but can’t spilt the other integer into two new integers. Because 1 can’t be divide into two new integers. Hence B is not able to make any moves and A wins.

Input: N = 1, M = 5
Output:
Explanation: It can be verified that it is not possible to win for Player A, If both the player plays optimally.

Approach: Implement the idea below to solve the problem

The problem is based on Game Theory and can be solved by using some observations. The key observation is, it is only possible to win for player A, when either N or M is even.

Steps were taken to solve the problem:

• If (N % 2 == 0) output A.
• else if (M % 2 == 0) output A.
• else output B.

Below is the code to implement the approach:

C++

 `// C++ code to implement the approach`   `#include ` `using` `namespace` `std;`   `// Function for output the winner` `void` `Find_Winner(``int` `N, ``int` `M)` `{`   `    ``if` `(N % 2 == 0) {` `        ``cout << ``"A"` `<< endl;` `    ``}` `    ``else` `if` `(M % 2 == 0) {` `        ``cout << ``"A"` `<< endl;` `    ``}` `    ``else` `{` `        ``cout << ``"B"` `<< endl;` `    ``}` `}`   `int` `main() {`   `    ``int` `N = 1;` `    ``int` `M = 5;`   `    ``// Function call` `    ``Find_Winner(N, M);` `}`

Java

 `// Java code to implement the approach`   `import` `java.io.*;` `import` `java.lang.*;` `import` `java.util.*;`   `class` `GFG {`   `    ``// Driver Function` `    ``public` `static` `void` `main(String[] args)` `        ``throws` `java.lang.Exception` `    ``{` `        ``int` `N = ``1``;` `        ``int` `M = ``5``;`   `        ``// Function call` `        ``Find_Winner(N, M);` `    ``}`   `    ``// Function for output the winner` `    ``static` `void` `Find_Winner(``int` `N, ``int` `M)` `    ``{`   `        ``if` `(N % ``2` `== ``0``) {` `            ``System.out.println(``"A"``);` `        ``}` `        ``else` `if` `(M % ``2` `== ``0``) {` `            ``System.out.println(``"A"``);` `        ``}` `        ``else` `{`   `            ``System.out.println(``"B"``);` `        ``}` `    ``}` `}`

Python3

 `# Python code to implement the approach`   `# Function for outputting the winner` `def` `find_winner(N, M):` `    ``if` `N ``%` `2` `=``=` `0``:` `        ``print``(``"A"``)` `    ``elif` `M ``%` `2` `=``=` `0``:` `        ``print``(``"A"``)` `    ``else``:` `        ``print``(``"B"``)`     `N ``=` `1` `M ``=` `5` `# Function call` `find_winner(N, M)`   `# This Code is Contributed by Prasad Kandekar(prasad264)`

Javascript

 `// JS code to implement the approach`   `// Function for output the winner` `function` `Find_Winner( N, M)` `{`   `    ``if` `(N % 2 == 0) {` `        ``console.log(``"A"` `+ ``"
"``);` `    ``}` `    ``else` `if` `(M % 2 == 0) {` `        ``console.log(``"A"` `+ ``"
"``);` `    ``}` `    ``else` `{` `        ``console.log(``"B"` `+ ``"
"``);` `    ``}` `}`   `let N = 1;` `let M = 5;`   `// Function call` `Find_Winner(N, M);`

C#

 `// C# code to implement the approach`   `using` `System;`   `public` `class` `GFG {` `    ``// Function for output the winner` `    ``public` `static` `void` `FindWinner(``int` `N, ``int` `M)` `    ``{` `        ``if` `(N % 2 == 0) {` `            ``Console.WriteLine(``"A"``);` `        ``}` `        ``else` `if` `(M % 2 == 0) {` `            ``Console.WriteLine(``"A"``);` `        ``}` `        ``else` `{` `            ``Console.WriteLine(``"B"``);` `        ``}` `    ``}`   `    ``public` `static` `void` `Main()` `    ``{` `        ``int` `N = 1;` `        ``int` `M = 5;`   `        ``// Function call` `        ``FindWinner(N, M);` `    ``}` `}`

Output

`B`

Time Complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles