# Find the winner in nim-game

You are given an array A[] of n-elements. 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 nim-game.

Note : Each players play game alternately. Find out winner if both of the players play optimally. Alice starts the game first. In case case one-element 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 non-zero:__ 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 non-zero.__ Now we can prove that Alice can always find an element to remove such that XOR of remaining elements of array will be non-zero.

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 (n-times) = 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 even-size array such that after its removal XOR of remaining elements in non-zero.

__Let size of array is even and XOR of array is non-zero.__ 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 non-zero. 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";

## C++

`// CPP to find nim-game winner ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to find winner of NIM-game ` `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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java to find nim-game winner ` `class` `GFG { ` ` ` ` ` `// function to find winner of NIM-game ` ` ` `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. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find nim-game winner ` ` ` `# Function to find winner of NIM-game ` `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. ` |

*chevron_right*

*filter_none*

## C#

`// C# to find nim-game winner ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function to find winner of NIM-game ` ` ` `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. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP to find nim-game winner ` ` ` `// function to find ` `// winner of NIM-game ` `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. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

Winner = Alice

## Recommended Posts:

- Find the winner of the game with N piles of boxes
- 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 | Sprague-Grundy
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Find two numbers from their sum and XOR
- Find position of the only set bit
- Given a set, find XOR of the XOR's of all subsets.
- Find most significant set bit of a number
- Find the node whose xor with x gives minimum value
- Find the values of X and Y in the Given Equations
- Find all possible triangles with XOR of sides zero
- Find a value whose XOR with given number is maximum
- Find k numbers which are powers of 2 and have sum N | Set 1
- Find a sub matrix with maximum XOR

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.