In Game of Nim, two players take turns removing objects from heaps or the pile of stones.

Suppose two players A and B are playing the game. Each is allowed to take only one stone from the pile. The player who picks the last stone of the pile will win the game. Given **N** the number of stones in the pile, the task is to find the winner, if player A starts the game.

**Examples :**

Input : N = 3. Output : Player A Player A remove stone 1 which is at the top, then Player B remove stone 2 and finally player A removes the last stone. Input : N = 15. Output : Player A

For N = 1, player A will remove the only stone from the pile and wins the game.

For N = 2, player A will remove the first stone and then player B remove the second or the last stone. So player B will win the game.

So, we can observe player A wins when N is odd and player B wins when N is even.

Below is the implementation of this approach:

## C++

`// C++ program for Game of Nim with removal ` `// of one stone allowed. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Return true if player A wins, ` `// return false if player B wins. ` `bool` `findWinner(` `int` `N) ` `{ ` ` ` `// Checking the last bit of N. ` ` ` `return` `N&1; ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `int` `N = 15; ` ` ` `findWinner(N)? (cout << ` `"Player A"` `;): ` ` ` `(cout << ` `"Player B"` `;); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// JAVA Code For Game of Nim with ` `// removal of one stone allowed ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Return true if player A wins, ` ` ` `// return false if player B wins. ` ` ` `static` `int` `findWinner(` `int` `N) ` ` ` `{ ` ` ` `// Checking the last bit of N. ` ` ` `return` `N & ` `1` `; ` ` ` `} ` ` ` ` ` `/* Driver program to test above function */` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `N = ` `15` `; ` ` ` `if` `(findWinner(N)==` `1` `) ` ` ` `System.out.println(` `"Player A"` `); ` ` ` `else` ` ` `System.out.println(` `"Player B"` `); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by Arnav Kr. Mandal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code for Game of Nim with ` `# removal of one stone allowed. ` ` ` `# Return true if player A wins, ` `# return false if player B wins. ` `def` `findWinner( N ): ` ` ` ` ` `# Checking the last bit of N. ` ` ` `return` `N & ` `1` ` ` `# Driven Program ` `N ` `=` `15` `print` `(` `"Player A"` `if` `findWinner(N) ` `else` `"Player B"` `) ` ` ` `# This code is contributed by "Sharad_Bhardwaj". ` |

*chevron_right*

*filter_none*

## C#

`// C# Code For Game of Nim with ` `// removal of one stone allowed ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Return true if player A wins, ` ` ` `// return false if player B wins. ` ` ` `static` `int` `findWinner(` `int` `N) ` ` ` `{ ` ` ` `// Checking the last bit of N. ` ` ` `return` `N & 1; ` ` ` `} ` ` ` ` ` `/* Driver program to test above function */` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `N = 15; ` ` ` ` ` `if` `(findWinner(N) == 1) ` ` ` `Console.Write(` `"Player A"` `); ` ` ` `else` ` ` `Console.Write(` `"Player B"` `); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for Game of ` `// Nim with removal of one ` `// stone allowed. ` ` ` `// Return true if player A wins, ` `// return false if player B wins. ` `function` `findWinner(` `$N` `) ` `{ ` ` ` `// Checking the last bit of N. ` `return` `$N` `&1; ` `} ` ` ` `// Driver Code ` `$N` `= 15; ` ` ` `if` `(findWinner(` `$N` `)) ` `echo` `"Player A"` `; ` `else` `echo` `"Player B"` `; ` ` ` `// This code is contributed by vt_m. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

Player A

**Time Complexity: **O(1).

This article is contributed by **Anuj Chauhan**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Combinatorial Game Theory | Set 2 (Game of Nim)
- A modified game of Nim
- Variation in Nim Game
- Find the winner in nim-game
- Number of ways for playing first move optimally in a NIM game
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Divisibility by 64 with removal of bits allowed
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Find position of given term in a series formed with only digits 4 and 7 allowed
- Check if one of the numbers is one's complement of the other
- Optimal Strategy for a Game | DP-31
- Combinatorial Game Theory | Set 1 (Introduction)
- Combinatorial Game Theory | Set 4 (Sprague - Grundy Theorem)
- Minimax Algorithm in Game Theory | Set 3 (Tic-Tac-Toe AI - Finding optimal move)
- Minimax Algorithm in Game Theory | Set 1 (Introduction)
- Minimax Algorithm in Game Theory | Set 2 (Introduction to Evaluation Function)

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.