Number of ways for playing first move optimally in a NIM game
Two players A and B are playing NIM Game with each other. Both are playing optimally. Player A starts the game. The task is to find the number of ways of playing 1st move for A to ensure a winning strategy for A if possible, otherwise, print -1.
Examples:
Input: arr[] = {1, 2, 3}
Output: -1
There is no winning strategy for A no matter how optimally he plays.
Input: arr[] = {2, 4, 5}
Output: 1
In order to play optimally, A will pick one coin from first pile and that’s the only optimal move.
Approach:
- First check who will win the game by taking XOR of all the array elements if XOR is zero then no matter how optimally A play, A will always lose. If XOR is non-zero then go to Step 2.
- We will check for every pile if we can remove some coins from that pile so that after this move, XOR of all the array elements will be zero. So for all piles, one by one we will take xor of all remaining elements of the array and will check if XOR value is greater than the number of coins in the pile. If so, it is not possible to play the first move using this pile because we can only remove coins from the pile in a move, and cannot add coins. Otherwise, we will increment the number of ways.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int xorArray( int arr[], int n)
{
int res = 0;
for ( int i = 0; i < n; i++)
res = res ^ arr[i];
return res;
}
int getTotalWays( int arr[], int n)
{
int xorArr = xorArray(arr, n);
if (xorArr == 0)
return -1;
int numberOfWays = 0;
for ( int i = 0; i < n; i++) {
int requiredCoins = xorArr ^ arr[i];
if (requiredCoins < arr[i])
numberOfWays++;
}
return numberOfWays;
}
int main()
{
int arr[] = { 3, 4, 4, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << getTotalWays(arr, n);
return 0;
}
|
Java
class GFG
{
static int xorArray( int arr[], int n)
{
int res = 0 ;
for ( int i = 0 ; i < n; i++)
res = res ^ arr[i];
return res;
}
static int getTotalWays( int arr[], int n)
{
int xorArr = xorArray(arr, n);
if (xorArr == 0 )
return - 1 ;
int numberOfWays = 0 ;
for ( int i = 0 ; i < n; i++)
{
int requiredCoins = xorArr ^ arr[i];
if (requiredCoins < arr[i])
numberOfWays++;
}
return numberOfWays;
}
public static void main(String[] args)
{
int arr[] = { 3 , 4 , 4 , 2 };
int n =arr.length;
System.out.println(getTotalWays(arr, n));
}
}
|
Python3
def xorArray(arr, n):
res = 0
for i in range ( 0 , n):
res = res ^ arr[i]
return res
def getTotalWays(arr, n):
xorArr = xorArray(arr, n)
if xorArr = = 0 :
return - 1
numberOfWays = 0
for i in range ( 0 , n):
requiredCoins = xorArr ^ arr[i]
if requiredCoins < arr[i]:
numberOfWays + = 1
return numberOfWays
if __name__ = = "__main__" :
arr = [ 3 , 4 , 4 , 2 ]
n = len (arr)
print (getTotalWays(arr, n))
|
C#
using System;
class GFG
{
static int xorArray( int []arr, int n)
{
int res = 0;
for ( int i = 0; i < n; i++)
res = res ^ arr[i];
return res;
}
static int getTotalWays( int []arr, int n)
{
int xorArr = xorArray(arr, n);
if (xorArr == 0)
return -1;
int numberOfWays = 0;
for ( int i = 0; i < n; i++)
{
int requiredCoins = xorArr ^ arr[i];
if (requiredCoins < arr[i])
numberOfWays++;
}
return numberOfWays;
}
static public void Main ()
{
int []arr = { 3, 4, 4, 2 };
int n = arr.Length;
Console.Write(getTotalWays(arr, n));
}
}
|
PHP
<?php
function xorArray( $arr , $n )
{
$res = 0;
for ( $i = 0; $i < $n ; $i ++)
$res = $res ^ $arr [ $i ];
return $res ;
}
function getTotalWays( $arr , $n )
{
$xorArr = xorArray( $arr , $n );
if ( $xorArr == 0)
return -1;
$numberOfWays = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$requiredCoins = $xorArr ^ $arr [ $i ];
if ( $requiredCoins < $arr [ $i ])
$numberOfWays ++;
}
return $numberOfWays ;
}
$arr = array (3, 4, 4, 2 );
$n = sizeof( $arr );
echo getTotalWays( $arr , $n );
?>
|
Javascript
<script>
function xorArray(arr, n)
{
let res = 0;
for (let i = 0; i < n; i++)
res = res ^ arr[i];
return res;
}
function getTotalWays(arr, n)
{
let xorArr = xorArray(arr, n);
if (xorArr == 0)
return -1;
let numberOfWays = 0;
for (let i = 0; i < n; i++) {
let requiredCoins = xorArr ^ arr[i];
if (requiredCoins < arr[i])
numberOfWays++;
}
return numberOfWays;
}
let arr = [ 3, 4, 4, 2 ];
let n = arr.length;
document.write(getTotalWays(arr, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
18 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...