Related Articles
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++

 `// C++ program for the``// above approach` `#include ``using` `namespace` `std;` `// Function to check which``// player wins the game``void` `checkWinner(``int` `arr[], ``int` `N)``{``    ``// Stores the difference between``    ``// +ve and -ve array elements``    ``int` `diff = 0;` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < N; i++) {``        ``// Update diff``        ``diff -= arr[i];``    ``}` `    ``// Checks if diff is even``    ``if` `(diff % 2 == 0) {``        ``cout << ``"A"``;``    ``}``    ``else` `{``        ``cout << ``"B"``;``    ``}``}` `// Driver Code``int` `main()``{``    ``// Given Input``    ``int` `arr[] = { 1, 2 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);` `    ``// Function call to check``    ``// which player wins the game``    ``checkWinner(arr, N);` `    ``return` `0;``}`

## Python3

 `# Python3 program for the``# above approach` `# Function to check which``# player wins the game``def` `checkWinner(arr, N):` `    ``# Stores the difference between``    ``# +ve and -ve array elements``    ``diff ``=` `0` `    ``# Traverse the array``    ``for` `i ``in` `range``(N):``        ``# Update diff``        ``diff ``-``=` `arr[i]` `    ``# Checks if diff is even``    ``if` `(diff ``%` `2` `=``=` `0``):``        ``print``(``"A"``)` `    ``else``:``        ``print``(``"B"``)` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``# Given Input``    ``arr ``=` `[``1``, ``2``]``    ``N ``=` `len``(arr)` `    ``# Function call to check``    ``# which player wins the game``    ``checkWinner(arr, N)` `    ``# This code is contributed by ukasp.`

## Java

 `// Java program for the``// above approach``import` `java.util.*;` `class` `GFG``{``// Function to check which``// player wins the game``static` `void` `checkWinner(``int` `arr[], ``int` `N)``{``    ``// Stores the difference between``    ``// +ve and -ve array elements``    ``int` `diff = ``0``;` `    ``// Traverse the array``    ``for` `(``int` `i = ``0``; i < N; i++) {``        ``// Update diff``        ``diff -= arr[i];``    ``}` `    ``// Checks if diff is even``    ``if` `(diff % ``2` `== ``0``) {``        ``System.out.println(``"A"``);``    ``}``    ``else` `{``        ``System.out.println(``"B"``);``    ``}``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``// Given Input``    ``int` `arr[] = { ``1``, ``2` `};``    ``int` `N = arr.length;``    ``// Function call to check``    ``// which player wins the game``    ``checkWinner(arr, N);``}``}` `// This code is contributed by Stream-Cipher`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to check which``// player wins the game``static` `void` `checkWinner(``int``[] arr, ``int` `N)``{``    ` `    ``// Stores the difference between``    ``// +ve and -ve array elements``    ``int` `diff = 0;` `    ``// Traverse the array``    ``for``(``int` `i = 0; i < N; i++)``    ``{``        ` `        ``// Update diff``        ``diff -= arr[i];``    ``}` `    ``// Checks if diff is even``    ``if` `(diff % 2 == 0)``    ``{``        ``Console.Write(``"A"``);``    ``}``    ``else``    ``{``        ``Console.Write(``"B"``);``    ``}``}` `// Driver Code``public` `static` `void` `Main()``{``    ` `    ``// Given Input``    ``int``[] arr = { 1, 2 };``    ``int` `N = arr.Length;``    ` `    ``// Function call to check``    ``// which player wins the game``    ``checkWinner(arr, N);``}``}` `// This code is contributed by sanjoy_62`

## Javascript

 ``
Output:
`B`

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA

My Personal Notes arrow_drop_up