You are given an array A[] of nelements. There are two players Alice and Bob. A Player can choose any of element from array and remove it. If the bitwise XOR of all remaining elements equals 0 after removal of selected element, then that player looses. This problem is variation of nimgame.
Note : Each players play game alternately. Find out winner if both of the players play optimally. Alice starts the game first. In case case oneelement in array consider its value as the XOR of array.
Examples :
Input : A[] = {3, 3, 2}
Output : Winner = Bob
Explanation : Alice can select 2 and remove it that make XOR of array equals to zero also if Alice choose 3 to remove than Bob can choose any of 2/3 and finally Alice have to make his steps.Input : A[] = {3, 3}
Output : Winner = Alice
Explanation : As XOR of array is already zero Alice can’t select any element to remove and hence Alice is winner.
Let’s start the solution step by step. We have total of three option for the XOR of array and this game.
 XOR of array is already 0: In this case Alice will unable to make a move and hence Alice is winner.

XOR of array is not zero: Now, in this case we have two options, either size of array will be odd or even.
 CASE A: If the array size is odd then for sure Bob will win the game.
 CASE B: If the array size is even then Alice will win the game.
Above conclusion can be proved with the help of mathematical induction.
Let A[] = {1} i.e. size of array is odd and XOR of array is nonzero: In this case Alice can select element 1 and then A[] will become empty and hence XOR of array can be considered as zero. Resulting Bob as winner.
Let size of array is even and XOR of array is nonzero. Now we can prove that Alice can always find an element to remove such that XOR of remaining elements of array will be nonzero.
To prove this lets start from the contradiction i.e. suppose whatever element you should choose XOR of remaining array must be zero.
So, let A1 Xor A2 Xor … An = X and n is even.
As per our contradiction hypothesis, Ai Xor X = 0 for 1<= i <= n.
Calculate XOR of all X Xor Ai (i.e. n equations),
After taking XOR of all n equations we have X Xor X…Xor X (ntimes) = 0 as N is even.
Now, also we have A1 Xor A2 Xor.. An = 0 but we know A1 Xor A2…Xor = X. This means we have at least one element in evensize array such that after its removal XOR of remaining elements in nonzero.
Let size of array is even and XOR of array is nonzero. Alice can not remove an element Ai such that xor of remaining number is zero, because that will make Bob win. Now, take the other case when the xor of remaining N?1 number is nonzero. As we know that N?1 is even and from the induction hypothesis, we can say that the position after the current move will be a winning position for Bob. Hence, it is a losing position for Alice.
int res = 0; for (int i = 1; i <= N; i++) { res ^= a[i]; } if (res == 0) return "ALice"; if (N%2 == 0) return "Alice"; else return "Bob";
// CPP to find nimgame winner #include <bits/stdc++.h> using namespace std;
// function to find winner of NIMgame string findWinner( int A[], int n)
{ int res = 0;
for ( int i = 0; i < n; i++)
res ^= A[i];
// case when Alice is winner
if (res == 0  n % 2 == 0)
return "Alice" ;
// when Bob is winner
else
return "Bob" ;
} // driver program int main()
{ int A[] = { 1, 4, 3, 5 };
int n = siseof(A) / sizeof (A[0]);
cout << "Winner = " << findWinner(A, n);
return 0;
} 
// Java to find nimgame winner class GFG {
// function to find winner of NIMgame
static String findWinner( int A[], int n)
{
int res = 0 ;
for ( int i = 0 ; i < n; i++)
res ^= A[i];
// case when Alice is winner
if (res == 0  n % 2 == 0 )
return "Alice" ;
// when Bob is winner
else
return "Bob" ;
}
//Driver code
public static void main (String[] args)
{
int A[] = { 1 , 4 , 3 , 5 };
int n =A.length;
System.out.print( "Winner = " + findWinner(A, n));
}
} // This code is contributed by Anant Agarwal. 
# Python3 program to find nimgame winner # Function to find winner of NIMgame def findWinner(A, n):
res = 0
for i in range (n):
res ^ = A[i]
# case when Alice is winner
if (res = = 0 or n % 2 = = 0 ):
return "Alice"
# when Bob is winner
else :
return "Bob"
# Driver code A = [ 1 , 4 , 3 , 5 ]
n = len (A)
print ( "Winner = " , findWinner(A, n))
# This code is contributed by Anant Agarwal. 
// C# to find nimgame winner using System;
class GFG {
// function to find winner of NIMgame
static String findWinner( int []A, int n)
{
int res = 0;
for ( int i = 0; i < n; i++)
res ^= A[i];
// case when Alice is winner
if (res == 0  n % 2 == 0)
return "Alice" ;
// when Bob is winner
else
return "Bob" ;
}
//Driver code
public static void Main ()
{
int []A = { 1, 4, 3, 5 };
int n =A.Length;
Console.WriteLine( "Winner = "
+ findWinner(A, n));
}
} // This code is contributed by vt_m. 
<?php // PHP to find nimgame winner // function to find // winner of NIMgame function findWinner( $A , $n )
{ $res = 0;
for ( $i = 0; $i < $n ; $i ++)
$res ^= $A [ $i ];
// case when Alice is winner
if ( $res == 0 or $n % 2 == 0)
return "Alice" ;
// when Bob is winner
else
return "Bob" ;
} // Driver Code $A = array (1, 4, 3, 5 );
$n = count ( $A );
echo "Winner = " , findWinner( $A , $n );
// This code is contributed by vt_m. ?> 
Output :
Winner = Alice
Recommended Posts:
 Find the winner of the Game
 Find the winner of the game with N piles of boxes
 Find the winner of the Game to Win by erasing any two consecutive similar alphabets
 Find the winner by adding Pairwise difference of elements in the array until Possible
 Predict the winner in Coin Game
 Predict the winner of the game  SpragueGrundy
 Winner in the RockPaperScissor game using Bit manipulation
 Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
 Find Kth smallest value for b such that a + b = a  b
 Find position of the only set bit
 Given a set, find XOR of the XOR's of all subsets.
 Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
 Find the value of N XOR'ed to itself K times
 Find two numbers from their sum and XOR
 Find value of kth bit in binary representation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vt_m