There are two players P1 and P2 and two piles of coins consisting of M and N coins respectively. At each turn, a player can choose only one of the piles out of these and discard the other one. This discarded pile cannot be used further in the game. The pile player chooses is further divided into two piles of non-zero parts. The player who cannot divide the pile i.e. the number of coins in the pile is < 2, loses the game. The task is to determine which player wins if P1 starts the game and both the players play optimally.
Examples:Â
Input: M = 4, N = 4Â
Output: Player 1Â
Explanation: Player 1 can choose any one of the piles as both contain the same number of coinsÂ
and then splits the chosen one (the one which is not chosen is discarded) into two piles with 1 coin each.Â
Now, player 2 is left with no move (as both the remaining piles contain a single coin eachÂ
which cannot be split into two groups of non-zero coins).Input: M = 1, N = 1Â
Output: Player 2Â
Approach: Simply check if any of the piles consist of an even number of coins. If yes then Player 1 wins else Player 2 wins.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to print the winner of the gamevoid findWinner(int M, int N){    if (M % 2 == 0 || N % 2 == 0)        cout << "Player 1";    else        cout << "Player 2";}Â
// Driver codeint main(){Â Â Â Â int M = 1, N = 2;Â Â Â Â findWinner(M, N);Â
    return 0;} |
Java
// Java implementation of the approachimport java.io.*;Â
class GFG {Â
    // Function to print the winner of the game    static void findWinner(int M, int N)    {        if (M % 2 == 0 || N % 2 == 0)            System.out.println("Player 1");        else            System.out.println("Player 2");    }Â
    // Driver code    public static void main(String[] args)    {        int M = 1, N = 2;        findWinner(M, N);    }}Â
// This code is contributed by ajit. |
Python3
# Python implementation of the approach# Function to print the winner of the game  def findWinner(M, N):    if (M % 2 == 0 or N % 2 == 0):        print("Player 1");    else:        print("Player 2");  # Driver codeM = 1;N = 2;findWinner(M, N);Â
Â
# This code contributed by PrinciRaj1992 |
C#
// C# implementation of the approachusing System;Â
class GFG {Â
    // Function to print the winner of the game    static void findWinner(int M, int N)    {        if (M % 2 == 0 || N % 2 == 0)            Console.WriteLine("Player 1");        else            Console.WriteLine("Player 2");    }Â
    // Driver code    static public void Main()    {        int M = 1, N = 2;        findWinner(M, N);    }}Â
// This code is contributed by Tushil.. |
PHP
<?php//PHP implementation of the approach// Function to print the winner of the gameÂ
function findWinner($M, $N){    if ($M % 2 == 0 || $N % 2 == 0)        echo "Player 1";    else        echo "Player 2";}Â
    // Driver code    $M = 1;    $N = 2;    findWinner($M, $N);Â
// This code is contributed by Tushil.?> |
Javascript
<script>Â
// Javascript implementation of the approachÂ
// Function to print the winner of the gamefunction findWinner(M, N){    if (M % 2 == 0 || N % 2 == 0)        document.write(("Player 1"));    else        document.write(("Player 2"));}Â
// Driver codevar M = 1, N = 2;findWinner(M, N);Â
// This code is contributed by rutvik_56.</script> |
Player 1
Time Complexity: O(1)Â
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
