# Predict the winner in Coin Game

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

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

There’s no move to make.

**Approach:** Simply check if any of the pile consists of 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 game ` `void` `findWinner(` `int` `M, ` `int` `N) ` `{ ` ` ` `if` `(M % 2 == 0 || N % 2 == 0) ` ` ` `cout << ` `"Player 1"` `; ` ` ` `else` ` ` `cout << ` `"Player 2"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `M = 1, N = 2; ` ` ` `findWinner(M, N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `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. ` |

*chevron_right*

*filter_none*

## 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 code ` `M ` `=` `1` `; ` `N ` `=` `2` `; ` `findWinner(M, N); ` ` ` ` ` `# This code contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `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.. ` |

*chevron_right*

*filter_none*

## 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. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Player 1

## Recommended Posts:

- Predict the winner of the game | Sprague-Grundy
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Find the winner of the Game
- Find the winner in nim-game
- Find the winner of the game with N piles of boxes
- Find the player who will win the Coin game
- Coin game of two corners (Greedy Approach)
- Find the winner of the Game to Win by erasing any two consecutive similar alphabets
- Optimal Strategy for a Game | Special Gold Coin
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Combinatorial Game Theory | Set 2 (Game of Nim)
- Coin Change | BFS Approach
- Find the winner by adding Pairwise difference of elements in the array until Possible
- A modified game of Nim

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.