Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmFind the player who wins the game of placing alternate +...

Find the player who wins the game of placing alternate + and – signs in front of array elements

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 <bits/stdc++.h>
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[0]);
 
    // 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




<script>
// JavaScript program for the above approach
 
// Function to check which
// player wins the game
function checkWinner(arr, N)
{
    // Stores the difference between
    // +ve and -ve array elements
    let diff = 0;
  
    // Traverse the array
    for (let i = 0; i < N; i++) {
        // Update diff
        diff -= arr[i];
    }
  
    // Checks if diff is even
    if (diff % 2 == 0) {
        document.write("A");
    }
    else {
        document.write("B");
    }
}
 
// Driver Code
 
     // Given Input
    let arr = [ 1, 2 ];
    let N = arr.length;
    // Function call to check
    // which player wins the game
    checkWinner(arr, N);
     
</script>


Output: 

B

 

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

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Ted Musemwa
As a software developer I’m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments