# Optimal Strategy for a Game | Special Gold Coin

Given a row of silver coins among which a special gold coin is present. Two players play the game, and with each move, a player has to remove a coin from either the left or the right end of the row and the player who removes the special coin wins the game. The task is to find the winner of the game.

**Examples:**

Input:str = “GSSS”

Output:First

The first player directly removes the special gold coin.

Input:str = “SGS”

Output:Second

Irrespective of which coin the first player removes, the special

gold coin becomes exposed and is removed by the second player.

**Approach:** It can be observed by taking a few examples that if the count of the silver coins is odd then the first player wins the game otherwise the player two wins the game. In special case when Gold coin is in corner, First player will be the winner regardless the count of silver coins.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the ` `// winner of the game ` `string getWinner(string str, ` `int` `len) ` `{ ` ` ` ` ` `// To store the count of silver coins ` ` ` `int` `total = 0; ` ` ` ` ` `if` `(str[0]==` `'G'` `||str[len-1]==` `'G'` `) ` ` ` `return` `"First"` `; ` ` ` `else` `{ ` ` ` `for` `(` `int` `i = 0; i < len; i++) { ` ` ` ` ` `// Update the position of ` ` ` `// the gold coin ` ` ` `if` `(str[i] == ` `'S'` `) { ` ` ` `total++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// First player will win the game ` ` ` `if` `((total % 2) == 1) ` ` ` `return` `"First"` `; ` ` ` `return` `"Second"` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"GSSS"` `; ` ` ` `int` `len = str.length(); ` ` ` ` ` `cout << getWinner(str, len); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the ` `// winner of the game ` `static` `String getWinner(String str, ` `int` `len) ` `{ ` ` ` ` ` `// To store the count of silver coins ` ` ` `int` `total = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < len; i++) ` ` ` `{ ` ` ` ` ` `// Update the position of ` ` ` `// the gold coin ` ` ` `if` `(str.charAt(i) == ` `'S'` `) ` ` ` `{ ` ` ` `total++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// First player will win the game ` ` ` `if` `((total % ` `2` `) == ` `1` `) ` ` ` `return` `"First"` `; ` ` ` `return` `"Second"` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String []args) ` `{ ` ` ` `String str = ` `"GSSS"` `; ` ` ` `int` `len = str.length(); ` ` ` ` ` `System.out.println(getWinner(str, len)); ` `} ` `} ` ` ` `// This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the ` `# winner of the game ` `def` `getWinner(string, length) : ` ` ` ` ` `# To store the count of silver coins ` ` ` `total ` `=` `0` `; ` ` ` `for` `i ` `in` `range` `(length) : ` ` ` ` ` `# Update the position of ` ` ` `# the gold coin ` ` ` `if` `(string[i] ` `=` `=` `'S'` `) : ` ` ` `total ` `+` `=` `1` `; ` ` ` ` ` `# First player will win the game ` ` ` `if` `((total ` `%` `2` `) ` `=` `=` `1` `) : ` ` ` `return` `"First"` `; ` ` ` `return` `"Second"` `; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `string ` `=` `"GSSS"` `; ` ` ` `length ` `=` `len` `(string); ` ` ` ` ` `print` `(getWinner(string, length)); ` ` ` `# This code is contributed by kanugargng ` |

*chevron_right*

*filter_none*

**Output:**

First

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Optimal Strategy for a Game | DP-31
- Optimal Strategy for a Game | Set 3
- Optimal strategy for a Game with modifications
- Puzzle 14 | (Strategy for a 2 Player Coin Game)
- Optimal Strategy for the Divisor game using Dynamic Programming
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Find the player who will win the Coin game
- Predict the winner in Coin Game
- Puzzle | (Round table coin game)
- Coin game of two corners (Greedy Approach)
- Minimax Algorithm in Game Theory | Set 3 (Tic-Tac-Toe AI - Finding optimal move)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Puzzle 62 | Gold or Silver
- Puzzle 4 | (Pay an employee using a 7 units gold rod?)
- Puzzle 20 | (5 Pirates and 100 Gold Coins)
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Minimum number of cuts required to pay salary from N length Gold Bar

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.