Find the player who wins the game of placing alternate + and – signs in front of array elements
Last Updated :
30 Apr, 2021
Given an array arr[] of length, N, the task is to find the winner of a game played by two players A and B optimally, by performing the following operations:
- Player A makes the first move.
- Players need to alternately place + and – sign in front of array elements in their turns.
- After having placed signs in front of all array elements, player A wins if the difference of all the elements is even.
- Otherwise, player B wins.
Examples:
Input: arr[] = {1, 2}
Output: B
Explanation:
All possible ways the game can be played out are:
(+1) – (+2) = -1
(?1) – (+2) = -3
(+1) – (-2) = 3
(-1) – (-2) = 1
Since the differences are odd in all the possibilities, B wins.
Input: arr[] = {1, 1, 2}
Output: A
Explanation:
All possible ways the game can be played out are:
(1) – (1) – (2) = -2
(1) – (1) – (-2) = 2
(1) – (-1) – (2) = 0
(1) – (-1) – (-2) = 4
(-1) – (1) – (2) = -4
(-1) – (1) – (-2) = 0
(-1) – (-1) – (2) = -2
(-1) – (-1) – (-2) = 4
Since the differences are even in all the possibilities, A wins.
Naive Approach: The simplest approach is to generate all the possible 2N combinations in which the signs can be placed in the array and check for each combination, check if player A can win or not. If found to be true for any permutation, then print A. Otherwise, player B wins.
Time Complexity: O(2N * N)
Auxiliary Space: O(1)
Efficient Approach: Follow the steps below to optimize the above approach:
- Initialize a variable, say diff, to store the sum of the array elements.
- Traverse the array arr[], over the range of indices [1, N], and update diff by subtracting arr[i] to it.
- If diff % 2 is found to be equal to 0, then print ‘A’. Otherwise, print ‘B’.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void checkWinner( int arr[], int N)
{
int diff = 0;
for ( int i = 0; i < N; i++) {
diff -= arr[i];
}
if (diff % 2 == 0) {
cout << "A" ;
}
else {
cout << "B" ;
}
}
int main()
{
int arr[] = { 1, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
checkWinner(arr, N);
return 0;
}
|
Python3
def checkWinner(arr, N):
diff = 0
for i in range (N):
diff - = arr[i]
if (diff % 2 = = 0 ):
print ( "A" )
else :
print ( "B" )
if __name__ = = "__main__" :
arr = [ 1 , 2 ]
N = len (arr)
checkWinner(arr, N)
|
Java
import java.util.*;
class GFG
{
static void checkWinner( int arr[], int N)
{
int diff = 0 ;
for ( int i = 0 ; i < N; i++) {
diff -= arr[i];
}
if (diff % 2 == 0 ) {
System.out.println( "A" );
}
else {
System.out.println( "B" );
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 };
int N = arr.length;
checkWinner(arr, N);
}
}
|
C#
using System;
class GFG{
static void checkWinner( int [] arr, int N)
{
int diff = 0;
for ( int i = 0; i < N; i++)
{
diff -= arr[i];
}
if (diff % 2 == 0)
{
Console.Write( "A" );
}
else
{
Console.Write( "B" );
}
}
public static void Main()
{
int [] arr = { 1, 2 };
int N = arr.Length;
checkWinner(arr, N);
}
}
|
Javascript
<script>
function checkWinner(arr, N)
{
let diff = 0;
for (let i = 0; i < N; i++) {
diff -= arr[i];
}
if (diff % 2 == 0) {
document.write( "A" );
}
else {
document.write( "B" );
}
}
let arr = [ 1, 2 ];
let N = arr.length;
checkWinner(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...