Given an array of N numbers. Two players X and Y play a game where at every step one player selects a number. One number can be selected only once. After all the numbers have been selected, player X wins if the absolute difference between the sum of numbers collected by X and Y is divisible by 4, else Y wins.
Note: Player X starts the game and numbers are selected optimally at every step.
Examples:
Input: a[] = {4, 8, 12, 16}
Output: X
X chooses 4
Y chooses 12
X chooses 8
Y chooses 16
|(4 + 8) – (12 + 16)| = |12 – 28| = 16 which is divisible by 4.
Hence, X wins
Input: a[] = {7, 9, 1}
Output: Y
Approach: The following steps can be followed to solve the problem:
- Initialize count0, count1, count2 and count3 to 0.
- Iterate for every number in the array and increase the above counters accordingly if a[i] % 4 == 0, a[i] % 4 == 1, a[i] % 4 == 2 or a[i] % 4 == 3.
- If count0, count1, count2 and count3 are all even numbers then X wins else Y will win.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int decideWinner( int a[], int n)
{
int count0 = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
for ( int i = 0; i < n; i++) {
if (a[i] % 4 == 0)
count0++;
else if (a[i] % 4 == 1)
count1++;
else if (a[i] % 4 == 2)
count2++;
else if (a[i] % 4 == 3)
count3++;
}
if (count0 % 2 == 0
&& count1 % 2 == 0
&& count2 % 2 == 0
&& count3 % 2 == 0)
return 1;
else
return 2;
}
int main()
{
int a[] = { 4, 8, 5, 9 };
int n = sizeof (a) / sizeof (a[0]);
if (decideWinner(a, n) == 1)
cout << "X wins" ;
else
cout << "Y wins" ;
return 0;
}
|
Java
class GFG
{
static int decideWinner( int []a, int n)
{
int count0 = 0 ;
int count1 = 0 ;
int count2 = 0 ;
int count3 = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (a[i] % 4 == 0 )
count0++;
else if (a[i] % 4 == 1 )
count1++;
else if (a[i] % 4 == 2 )
count2++;
else if (a[i] % 4 == 3 )
count3++;
}
if (count0 % 2 == 0 && count1 % 2 == 0 &&
count2 % 2 == 0 && count3 % 2 == 0 )
return 1 ;
else
return 2 ;
}
public static void main(String args[])
{
int []a = { 4 , 8 , 5 , 9 };
int n = a.length;
if (decideWinner(a, n) == 1 )
System.out.print( "X wins" );
else
System.out.print( "Y wins" );
}
}
|
Python3
def decideWinner(a, n):
count0 = 0
count1 = 0
count2 = 0
count3 = 0
for i in range (n):
if (a[i] % 4 = = 0 ):
count0 + = 1
elif (a[i] % 4 = = 1 ):
count1 + = 1
elif (a[i] % 4 = = 2 ):
count2 + = 1
elif (a[i] % 4 = = 3 ):
count3 + = 1
if (count0 % 2 = = 0 and count1 % 2 = = 0 and
count2 % 2 = = 0 and count3 % 2 = = 0 ):
return 1
else :
return 2
a = [ 4 , 8 , 5 , 9 ]
n = len (a)
if (decideWinner(a, n) = = 1 ):
print ( "X wins" )
else :
print ( "Y wins" )
|
C#
using System;
class GFG
{
static int decideWinner( int []a, int n)
{
int count0 = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
for ( int i = 0; i < n; i++)
{
if (a[i] % 4 == 0)
count0++;
else if (a[i] % 4 == 1)
count1++;
else if (a[i] % 4 == 2)
count2++;
else if (a[i] % 4 == 3)
count3++;
}
if (count0 % 2 == 0 && count1 % 2 == 0 &&
count2 % 2 == 0 && count3 % 2 == 0)
return 1;
else
return 2;
}
public static void Main()
{
int []a = { 4, 8, 5, 9 };
int n = a.Length;
if (decideWinner(a, n) == 1)
Console.Write( "X wins" );
else
Console.Write( "Y wins" );
}
}
|
PHP
<?php
function decideWinner( $a , $n )
{
$count0 = 0;
$count1 = 0;
$count2 = 0;
$count3 = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] % 4 == 0)
$count0 ++;
else if ( $a [ $i ] % 4 == 1)
$count1 ++;
else if ( $a [ $i ] % 4 == 2)
$count2 ++;
else if ( $a [ $i ] % 4 == 3)
$count3 ++;
}
if ( $count0 % 2 == 0 && $count1 % 2 == 0 &&
$count2 % 2 == 0 && $count3 % 2 == 0)
return 1;
else
return 2;
}
$a = array ( 4, 8, 5, 9 );
$n = count ( $a );
if (decideWinner( $a , $n ) == 1)
echo "X wins" ;
else
echo "Y wins" ;
?>
|
Javascript
<script>
function decideWinner(a , n) {
var count0 = 0;
var count1 = 0;
var count2 = 0;
var count3 = 0;
for (i = 0; i < n; i++) {
if (a[i] % 4 == 0)
count0++;
else if (a[i] % 4 == 1)
count1++;
else if (a[i] % 4 == 2)
count2++;
else if (a[i] % 4 == 3)
count3++;
}
if (count0 % 2 == 0 && count1 % 2 == 0 && count2 % 2 == 0 && count3 % 2 == 0)
return 1;
else
return 2;
}
var a = [ 4, 8, 5, 9 ];
var n = a.length;
if (decideWinner(a, n) == 1)
document.write( "X wins" );
else
document.write( "Y wins" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)