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 are the steps to implement the above approach:
- Define a function getWinner that takes a string str and an integer len as input.
- Initialize a variable total to 0 to keep track of the number of silver coins.
- Check if the first or last character of the string is ‘G‘. If it is, the first player can pick up the gold coin on the first move and win. Return “First” in this case.
- Otherwise, loop through each character in the string and increment the total variable every time a silver coin is encountered.
- If the number of silver coins is odd, the first player can pick up the last silver coin and win. Return “First” in this case.
- Otherwise, if the number of silver coins is even, the second player can always win by picking up the last silver coin after the first player’s move. Return “Second” in this case.
- If none of the above conditions are met, the function returns “Second” by default.
- In the main function, define a string str and its length len.
- Call the getWinner function with str and len as arguments and print the output.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
import java.util.*;
class GFG
{
static String getWinner(String str, int len)
{
int total = 0 ;
for ( int i = 0 ; i < len; i++)
{
if (str.charAt(i) == 'S' )
{
total++;
}
}
if ((total % 2 ) == 1 )
return "First" ;
return "Second" ;
}
public static void main(String []args)
{
String str = "GSSS" ;
int len = str.length();
System.out.println(getWinner(str, len));
}
}
|
Python3
def getWinner(string, length) :
total = 0 ;
for i in range (length) :
if (string[i] = = 'S' ) :
total + = 1 ;
if ((total % 2 ) = = 1 ) :
return "First" ;
return "Second" ;
if __name__ = = "__main__" :
string = "GSSS" ;
length = len (string);
print (getWinner(string, length));
|
C#
using System;
class GFG
{
static String getWinner(String str, int len)
{
int total = 0;
for ( int i = 0; i < len; i++)
{
if (str[i] == 'S' )
{
total++;
}
}
if ((total % 2) == 1)
return "First" ;
return "Second" ;
}
public static void Main( string []args)
{
string str = "GSSS" ;
int len = str.Length;
Console.WriteLine(getWinner(str, len));
}
}
|
Javascript
<script>
function getWinner(str, len)
{
var total = 0;
if (str[0] == 'G' || str[len - 1] == 'G' )
return "First" ;
else
{
for ( var i = 0; i < len; i++)
{
if (str[i] == 'S' )
{
total++;
}
}
if ((total % 2) == 1)
return "First" ;
return "Second" ;
}
}
var str = "GSSS" ;
var len = str.length;
document.write(getWinner(str, len));
</script>
|
Time Complexity: O(n) where n is the size of the string.
Auxiliary Space: O(1)
Last Updated :
24 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...