# Game of stones

Given an integer *N* which is the number of stones in a pile and the game of stones is being played between you and your friend, the task is to find whether you’ll win or not. A player (on his/her turn) can remove 1 or 2 or 3 coins, the game continues in alternating turns. The one who is not able to make the move loses the game. In other words, the player who removes last set of coins always wins. Print *YES* if you can win the game else print *NO*.

**Examples:**

Input:N = 4

Output:NO

If there are 4 stones in the bag, then you will never win the game. No matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend. Hence, you are not able to make the move.

Input:N = 3

Output:YES

Yes you can win the game, you just remove all 3 stones in your turn.

**Approach:**

- First consider base cases, i.e for
*n = [1, 2, 3]*. In these cases you always win as you can pick all stone and your friend is not able to make the move. - If
*n = 4*, you will lose. Because no matter how many you take, you will leave some stones behind for your friend to take and win the game. So in order to win, you have to ensure that you never reach the situation where there are exactly four stones left when your turn comes up. - Similarly, if there are
*5, 6, or 7*stones you can win by taking just enough to leave*4*stones for your friend. But if there are 8 stones on the pile, you will lose because regardless whether you pick*1, 2 or 3*stones, your friend can pick*3, 2 or 1*stone to ensure that, again, 4 stones will be left for you . - It is obvious that the same pattern repeats, so, if
*n % 4 == 0*then you will always lose.

Below is the implementation of the above approach:

## C++

`// C++ program of game of stones ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns true if u win ` `bool` `checkWin(` `int` `n) ` `{ ` ` ` `if` `(n % 4 != 0) ` ` ` `return` `true` `; ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// n is number of stones ` ` ` `int` `n = 4; ` ` ` ` ` `if` `(checkWin(n)) ` ` ` `cout << ` `"YES"` `<< endl; ` ` ` `else` ` ` `cout << ` `"NO"` `<< endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program of game of stones ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// Function that returns true if u win ` `static` `boolean` `checkWin(` `int` `n) ` `{ ` ` ` `if` `(n % ` `4` `!= ` `0` `) ` ` ` `return` `true` `; ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` ` ` `// n is number of stones ` ` ` `int` `n = ` `4` `; ` ` ` ` ` `if` `(checkWin(n)) ` ` ` `System.out.println(` `"YES"` `); ` ` ` `else` ` ` `System.out.println( ` `"NO"` `); ` ` ` ` ` `} ` `//This code is contributed by akt_mit ` `} ` |

*chevron_right*

*filter_none*

## python 3

`# Python3 program of game of stones ` ` ` `# Function that returns true if u win ` `def` `checkWin( n): ` ` ` ` ` `if` `(n ` `%` `4` `!` `=` `0` `): ` ` ` `return` `True` ` ` `return` `False` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `# n is number of stones ` ` ` `n ` `=` `4` ` ` ` ` `if` `(checkWin(n)): ` ` ` `print` `( ` `"YES"` `) ` ` ` `else` `: ` ` ` `print` `(` `"NO"` `) ` |

*chevron_right*

*filter_none*

## C#

`//C# program of game of stones ` `using` `System; ` ` ` `public` `class` `GFG{ ` `// Function that returns true if u win ` `static` `bool` `checkWin(` `int` `n) ` `{ ` ` ` `if` `(n % 4 != 0) ` ` ` `return` `true` `; ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` ` ` ` ` `static` `public` `void` `Main (){ ` ` ` ` ` `// n is number of stones ` ` ` `int` `n = 4; ` ` ` ` ` `if` `(checkWin(n)) ` ` ` `Console.WriteLine(` `"YES"` `); ` ` ` `else` ` ` `Console.WriteLine( ` `"NO"` `); ` ` ` ` ` `} ` `//This code is contributed by ajit ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program of game of stones ` ` ` `// Function that returns ` `// true if u win ` `function` `checkWin(` `$n` `) ` `{ ` ` ` `if` `(` `$n` `% 4 != 0) ` ` ` `return` `true; ` ` ` `return` `false; ` `} ` ` ` `// Driver code ` ` ` `// n is number of stones ` `$n` `= 4; ` ` ` `if` `(checkWin(` `$n` `)) ` ` ` `echo` `"YES"` `,` `"\n"` `; ` `else` ` ` `echo` `"NO"` `,` `"\n"` `; ` ` ` `// This code is contributed ` `// by ANKITRAI1 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

NO

## Recommended Posts:

- Water Game
- Even-odd turn game with two integers
- Minimum Players required to win the game
- Python program for word guessing game
- Sum of all mersenne numbers present in an array
- Sum of all perfect numbers present in an array
- Difference between while and do-while loop in C, C++, Java
- Sum of all palindrome numbers present in an Array
- Difference between High Level and Low level languages
- Count of all possible values of X such that A % X = B
- Python | Append Odd element twice
- Python | Scramble words from a text file
- Python program for word guessing game
- Reorder the position of the words in alphabetical order

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.