Open In App

Find the winner of the game based on the given moves

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:

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:

Below is the code to implement the approach:




// C++ code to implement the approach
 
#include <bits/stdc++.h>
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 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");
        }
    }
}




# 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)




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




// 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)


Article Tags :