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