Predict the winner of a game by converting 0 to 1 turn by turn following the given rules
Given a binary string str consisting of only 0 and 1, where 0 represent an unoccupied position and 1 represent an occupied position. Two players A and B have to occupy an unoccupied position (convert 0 to 1) turn by turn following the given rules:
- The player can only occupy the unoccupied position
- The player can only move to its adjacent position in every turn
If a player is not able to make a move on the turn he loses. Predict the winner of the game if both play optimally given that A makes the first move.
Example:
Input: str=”1000″
Output: A
Explanation: A makes first move and occupy position at 2nd index of the string(0 based index) then no matter which position B choose there will be only a single unoccupied position left which will be occupied by A in next turn and B can’t make further move hence A wins.
Input: str=”1001″
Output: B
Approach:
- Find the length of the two longest substrings of initial unoccupied positions (i.e. substring of 0s).
- If the length of the largest substring is even, then B will be the winner irrespective of the length of the second-largest substring. This is because A will always occupy the middle slot of the largest substring hence in this case it will have the same number of slots for the move as left for B in the given substring. A needs more slots than B to win the game, while in this case, both have an equal number of slots hence A will lose.
- If the length of the largest substring is odd then there can be two cases:
- If the length of the second-largest substring is less than the number of slots of A in the largest substring (i.e. (n/2) + 1 where n is the size of the substring) then A will be the winner since B will always have a lesser number of slots to than A irrespective of his initial decision (to choose between largest or second-largest substring).
- Else, B will be the winner as B can choose the second-largest substring to make the initial move and will have more slots available for further moves than A.
- Print the answer according to the above observation.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <math.h>
using namespace std;
string predictWinner(string s)
{
int n = s.length();
int max1 = 0, max2 = 0;
int curr = 0;
for ( int i = 0; i < n; i++) {
if (s[i] == '0' ) {
curr++;
}
else {
if (max1 <= curr) {
max2 = max1;
max1 = curr;
}
else if ((curr < max1)
&& (curr > max2)) {
max2 = curr;
}
curr = 0;
}
}
if (curr > 0) {
if (max1 <= curr) {
max2 = max1;
max1 = curr;
}
else if ((curr < max1)
&& (curr > max2)) {
max2 = curr;
}
}
if (max1 % 2 == 0) {
return "B" ;
}
int left = ceil (( float )max1 / 2);
if (left > max2) {
return "A" ;
}
return "B" ;
}
int main()
{
string s = "1000" ;
string ans = predictWinner(s);
cout << ans;
return 0;
}
|
C
#include <math.h>
#include <stdio.h>
char predictWinner( char s[], int n)
{
int max1 = 0, max2 = 0;
int curr = 0;
for ( int i = 0; i < n; i++) {
if (s[i] == '0' ) {
curr++;
}
else {
if (max1 <= curr) {
max2 = max1;
max1 = curr;
}
else if ((curr < max1) && (curr > max2)) {
max2 = curr;
}
curr = 0;
}
}
if (curr > 0) {
if (max1 <= curr) {
max2 = max1;
max1 = curr;
}
else if ((curr < max1) && (curr > max2)) {
max2 = curr;
}
}
if (max1 % 2 == 0) {
return 'B' ;
}
int left = ceil (( float )max1 / 2);
if (left > max2) {
return 'A' ;
}
return 'B' ;
}
int main()
{
char s[] = "1000" ;
int n = 4;
char ans = predictWinner(s, n);
printf ( "%c" , ans);
return 0;
}
|
Java
import java.lang.Math;
import java.util.*;
public class GeeksForGeeks
{
public static String predictWinner(String s)
{
int n = s.length();
int max1 = 0 , max2 = 0 ;
int curr = 0 ;
for ( int i = 0 ; i < n; i++) {
if (s.charAt(i) == '0' ) {
curr++;
}
else {
if (max1 <= curr) {
max2 = max1;
max1 = curr;
}
else if ((curr < max1) && (curr > max2)) {
max2 = curr;
}
curr = 0 ;
}
}
if (curr > 0 ) {
if (max1 <= curr) {
max2 = max1;
max1 = curr;
}
else if ((curr < max1) && (curr > max2)) {
max2 = curr;
}
}
if (max1 % 2 == 0 ) {
return "B" ;
}
int left = ( int )Math.ceil(( float )max1 / 2 );
if (left > max2) {
return "A" ;
}
return "B" ;
}
public static void main(String args[])
{
String s = "1000" ;
String ans = predictWinner(s);
System.out.println(ans);
}
}
|
Python
import math
def predictWinner(s, n):
max1 = 0
max2 = 0
curr = 0
i = 0
for i in range (n):
if s[i] = = '0' :
curr + = 1
else :
if max1 < = curr:
max2 = max1
max1 = curr
elif (curr < max1) and (curr > max2):
max2 = curr
curr = 0
if curr > 0 :
if max1 < = curr:
max2 = max1
max1 = curr
elif (curr < max1) and (curr > max2):
max2 = curr
if max1 % 2 = = 0 :
return "B"
left = math.ceil(max1 / 2 )
if left > max2:
return "A"
return "B"
s = "1000"
n = len (s)
ans = predictWinner(s, n)
print (ans)
|
C#
using System;
class GeeksForGeeks
{
static string predictWinner( string s)
{
int n = s.Length;
int max1 = 0, max2 = 0;
int curr = 0;
for ( int i = 0; i < n; i++) {
if (s[i] == '0' ) {
curr++;
}
else {
if (max1 <= curr) {
max2 = max1;
max1 = curr;
}
else if ((curr < max1) && (curr > max2)) {
max2 = curr;
}
curr = 0;
}
}
if (curr > 0) {
if (max1 <= curr) {
max2 = max1;
max1 = curr;
}
else if ((curr < max1) && (curr > max2)) {
max2 = curr;
}
}
if (max1 % 2 == 0) {
return "B" ;
}
int left = ( int )Math.Ceiling(( float )max1 / 2);
if (left > max2) {
return "A" ;
}
return "B" ;
}
public static void Main()
{
string s = "1000" ;
string ans = predictWinner(s);
Console.Write(ans);
}
}
|
Javascript
<script>
function predictWinner(s)
{
let n = s.length;
let max1 = 0, max2 = 0;
let curr = 0;
for (let i = 0; i < n; i++) {
if (s[i] == '0' ) {
curr++;
}
else {
if (max1 <= curr) {
max2 = max1;
max1 = curr;
}
else if ((curr < max1) && (curr > max2)) {
max2 = curr;
}
curr = 0;
}
}
if (curr > 0) {
if (max1 <= curr) {
max2 = max1;
max1 = curr;
}
else if ((curr < max1) && (curr > max2)) {
max2 = curr;
}
}
if (max1 % 2 == 0) {
return "B" ;
}
let left = Math.ceil(max1 / 2);
if (left > max2) {
return "A" ;
}
return "B" ;
}
let s = "1000" ;
let ans = predictWinner(s);
document.write(ans);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
17 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...