Predict the winner of a card game of removing K cards in each turn such that Bitwise AND of K and size of pile is 0
There are two players A and B and a pile of N cards stacked upon each other. The task is to find the winner of the game, assuming both players play optimally as per the following guidelines:
- Player A always begins the game and the players take alternate turns subsequently.
- In each turn, a player can remove K( 1 ? K ? N) cards if K & n = 0, where n is the size of the current pile.
- If a player cannot make a move at any point in the game, then that player loses, and the game ends.
Examples:
Input: N = 1
Output: B
Explanation:
A can only remove 1 card, but 1 & 1 = 1, so A is unable to make a move.
Hence, B wins the game.
Input: N = 4
Output: A
Explanation:
A will remove 3 cards as 3 & 4 = 0, now only 1 card is left and B cannot make a move.
Hence, A wins the game.
Approach: The idea is based on the observation that if the count of 1s in the binary representation of N, before a 0 is encountered, is odd then A wins the game. If no such combination of 1s and 0s exists throughout the binary string, then B wins. Follow the steps below to solve the problem:
- Initialize a variable countOne to store the count of 1.
- Convert N into its binary representation and store it in a string, binString.
- Traverse the string binString and do the following:
- If ‘1‘ is encountered, increment the countOne.
- If ‘0‘ is encountered, check if the countOne is odd or even, if the countOne is odd A wins, and break out of the loop, otherwise reset the countOne to 0 and continue traversing.
- If the whole string is traversed without breaking, then B wins.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findWinner( int N)
{
int onesBeforeZero = 0;
int flag = 1;
int binString[32];
int i = 0;
while (N > 0)
{
binString[i] = N % 2;
N = N / 2;
i++;
}
int l = sizeof (binString) /
sizeof (binString[0]);
for ( int j = 0; j < l; j++)
{
if (binString[j] == 1)
{
onesBeforeZero += 1;
}
else
{
if (onesBeforeZero & 1)
{
cout << "A" ;
flag = 0;
break ;
}
else
onesBeforeZero = 0;
}
}
if (flag == 1)
cout << "B" ;
}
int main()
{
int N = 4;
findWinner(N);
return 0;
}
|
C
#include <stdio.h>
void findWinner(unsigned long long N)
{
int onesBeforeZero = 0;
int flag = 1, j = 0;
char binString[32];
for ( int i = 31; i >= 0; i--)
{
unsigned long long temp = N >> i;
if (temp & 1)
binString[j] = '1' ;
else
binString[j] = '0' ;
j += 1;
}
for ( int i = 0; i < 32; i++)
{
if (binString[i] == '1' )
onesBeforeZero += 1;
else
{
if (onesBeforeZero & 1)
{
printf ( "A" );
flag = 0;
break ;
}
else
onesBeforeZero = 0;
}
}
if (flag == 1)
printf ( "B" );
}
int main()
{
unsigned long long N = 4;
findWinner(N);
return 0;
}
|
Java
class GFG{
static void findWinner( long N)
{
int onesBeforeZero = 0 , flag = 1 , j = 0 ;
String[] binString = new String[ 32 ];
for ( int i = 31 ; i >= 0 ; i--)
{
long temp = N >> i;
if ((temp & 1 ) == 1 )
binString[j] = "1" ;
else
binString[j] = "0" ;
j += 1 ;
}
for ( int i = 0 ; i < 32 ; i++)
{
if (binString[i] == "1" )
onesBeforeZero += 1 ;
else
{
if ((onesBeforeZero & 1 ) == 1 )
{
System.out.println( "A" );
flag = 0 ;
break ;
}
else
onesBeforeZero = 0 ;
}
}
if (flag == 1 )
System.out.println( "B" );
}
public static void main(String[] args)
{
long N = 4 ;
findWinner(N);
}
}
|
Python3
def findWinner(N):
onesBeforeZero = 0
flag = 1
binString = bin (N).replace( "0b" , "")
l = len (binString)
for j in range (l):
if binString[j] = = '1' :
onesBeforeZero + = 1
else :
if onesBeforeZero & 1 :
print ( "A" )
flag = 0
break
else :
onesBeforeZero = 0
if flag = = 1 :
print ( "B" )
N = 4
findWinner(N)
|
C#
using System;
class GFG{
static void findWinner( long N)
{
int onesBeforeZero = 0, flag = 1, j = 0;
String[] binString = new String[32];
for ( int i = 31; i >= 0; i--)
{
long temp = N >> i;
if ((temp & 1) == 1)
binString[j] = "1" ;
else
binString[j] = "0" ;
j += 1;
}
for ( int i = 0; i < 32; i++)
{
if (binString[i] == "1" )
onesBeforeZero += 1;
else
{
if ((onesBeforeZero & 1) == 1)
{
Console.WriteLine( "A" );
flag = 0;
break ;
}
else
onesBeforeZero = 0;
}
}
if (flag == 1)
Console.WriteLine( "B" );
}
public static void Main(String[] args)
{
long N = 4;
findWinner(N);
}
}
|
Javascript
<script>
function findWinner(N)
{
let onesBeforeZero = 0, flag = 1, j = 0;
let binString = [];
for (let i = 31; i >= 0; i--)
{
let temp = N >> i;
if ((temp & 1) == 1)
binString[j] = "1" ;
else
binString[j] = "0" ;
j += 1;
}
for (let i = 0; i < 32; i++)
{
if (binString[i] == "1" )
onesBeforeZero += 1;
else
{
if ((onesBeforeZero & 1) == 1)
{
document.write( "A" );
flag = 0;
break ;
}
else
onesBeforeZero = 0;
}
}
if (flag == 1)
document.write( "B" );
}
let N = 4;
findWinner(N);
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(log N)
Last Updated :
30 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...