Given a Binary String S. The task is to determine the winner of the game when two players play a game optimally with the string as per the given conditions:
- Player 1 always starts first.
- Two players take turns choosing a whole block of consecutive equal characters and deleting them from a given binary String S.
- Player 1 can choose only an odd number of consecutive equal characters and Player 2 can choose only an even number of consecutive equal characters. The player may choose nothing and count it as their turn only if it is impossible to choose anything.
- After all the characters are removed, the player with maximum scores wins the game and if scores are equal then print “-1”
Input: S = “1110011001010”
Output: Player 1
Explanation:
The selected characters will be in bold and Player 1’s score is Score_1 and Player 2’s score is Score_2 :
Turn 1 : (Player 1) “1110011001010” → “0011001010” Score_1 = 3
Turn 2 : (Player 2) “0011001010” → “00001010” Score_2 = 2
Turn 3 : (Player 1) “00001010”→ “0000010” Score_1 = 4
Turn 4: (Player 2) “0000010”
He cannot do anything as only one ‘1’ is present which is an odd number.
Also, he can’t choose the ‘0’s as they are odd (5 and 1), Therefore, Score_2 =2
Turn 5:(Player 1) “0000010”→ “000000” Score_1=5
Turn 6:(Player 2) “000000” → “” Score_2 = 2 (No ‘1’ was deleted in this turn)
Final scores: Score_1 = 5 and Score_2 = 2
Therefore, Player 1 wins.
Input : S = “11111101”
Output: Player 2
Explanation:
Turn 1 : (Player 1) “11111101” → “1111110” Score_1 = 3
Turn 2 : (Player 2) “1111110” → “0” Score_2 = 6
Turn 3 : (Player 1) “0” → “” Score_1 = 3
Final scores: Score_1 = 3 and Score_2 = 6
Therefore, Player 2 wins.
Approach:
- If we observe this game carefully, we understand that the only consecutive 1s are contributing to the scores of these players.
- Create a list to store the lengths of the consecutive 1s in the string.
- Sort the list in descending order.
- Now iterate over the list and if the list element is odd it will be added to the score of the Player 1 and if it is even it will be added to the score of the Player 2.
- Now if the score of the Player 1 is greater than the score of the Player 2 then print “Player 1” and if the score of the Player 2 is greater than the score of the Player 1 then print “Player 2”.
- Print “-1” if there is a tie i.e., scores are the same.
Below is the implementation of the above approach.
C++
#include<bits/stdc++.h>
using namespace std;
string gameMax(string S)
{
int N = S.length();
vector< int > list;
int one = 0;
for ( int i = 0; i < N; i++)
{
if (S[i] == '1' )
{
one++;
}
else
{
if (one != 0)
{
list.push_back(one);
}
one = 0;
}
}
if (one != 0)
{
list.push_back(one);
}
sort(list.begin(), list.end(),
greater< int >());
int score_1 = 0, score_2 = 0;
for ( int i = 0; i < list.size(); i++)
{
if (list[i] % 2 == 1)
{
score_1 += list[i];
}
else
{
score_2 += list[i];
}
}
if (score_1 == score_2)
return "-1" ;
return (score_1 > score_2) ? "Player 1" :
"Player 2" ;
}
int main()
{
string S = "11111101" ;
cout << gameMax(S);
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static String gameMax(String S)
{
int N = S.length();
List<Integer> list = new ArrayList<>();
int one = 0 ;
for ( int i = 0 ; i < N; i++) {
if (S.charAt(i) == '1' ) {
one++;
}
else {
if (one != 0 ) {
list.add(one);
}
one = 0 ;
}
}
if (one != 0 ) {
list.add(one);
}
Collections.sort(list,
Collections.reverseOrder());
int score_1 = 0 , score_2 = 0 ;
for ( int i = 0 ; i < list.size(); i++) {
if (list.get(i) % 2 == 1 ) {
score_1 += list.get(i);
}
else {
score_2 += list.get(i);
}
}
if (score_1 == score_2)
return "-1" ;
return (score_1 > score_2) ? "Player 1"
: "Player 2" ;
}
public static void main(String[] args)
{
String S = "11111101" ;
System.out.println(gameMax(S));
}
}
|
Python3
def gameMax(S):
N = len (S)
list = []
one = 0
for i in range (N):
if (S[i] = = '1' ):
one + = 1
else :
if (one ! = 0 ):
list .append(one)
one = 0
if (one ! = 0 ):
list .append(one)
list .sort(reverse = True )
score_1 = 0
score_2 = 0
for i in range ( len ( list )):
if ( list [i] % 2 = = 1 ):
score_1 + = list [i]
else :
score_2 + = list [i]
if (score_1 = = score_2):
return '-1'
if (score_1 > score_2):
return "Player 1"
else :
return "Player 2"
S = "11111101"
print (gameMax(S))
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static String gameMax(String S)
{
int N = S.Length;
List< int > list = new List< int >();
int one = 0;
for ( int i = 0; i < N; i++)
{
if (S[i] == '1' )
{
one++;
}
else
{
if (one != 0)
{
list.Add(one);
}
one = 0;
}
}
if (one != 0)
{
list.Add(one);
}
list.Sort();
list.Reverse();
int score_1 = 0, score_2 = 0;
for ( int i = 0; i < list.Count; i++)
{
if (list[i] % 2 == 1)
{
score_1 += list[i];
}
else
{
score_2 += list[i];
}
}
if (score_1 == score_2)
return "-1" ;
return (score_1 > score_2) ?
"Player 1" : "Player 2" ;
}
public static void Main(String[] args)
{
String S = "11111101" ;
Console.WriteLine(gameMax(S));
}
}
|
Javascript
<script>
function gameMax(S)
{
let N = S.length;
let list = [];
let one = 0;
for (let i = 0; i < N; i++)
{
if (S[i] == '1' )
{
one++;
}
else
{
if (one != 0)
{
list.push(one);
}
one = 0;
}
}
if (one != 0)
{
list.push(one);
}
list.sort();
list.reverse();
let score_1 = 0, score_2 = 0;
for (let i = 0; i < list.length; i++)
{
if (list[i] % 2 == 1)
{
score_1 += list[i];
}
else
{
score_2 += list[i];
}
}
if (score_1 == score_2)
return "-1" ;
return (score_1 > score_2) ?
"Player 1" : "Player 2" ;
}
let S = "11111101" ;
document.write(gameMax(S));
</script>
|
Output:
Player 2
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
24 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...