Predict the winner of the game on the basis of absolute difference of sum by selecting numbers

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++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to decide the winnerint decideWinner(int a[], int n){Â Â Â Â int count0 = 0;Â Â Â Â int count1 = 0;Â Â Â Â int count2 = 0;Â Â Â Â int count3 = 0;Â
    // Iterate for all numbers in the array    for (int i = 0; i < n; i++) {Â
        // Condition to countÂ
        // If mod gives 0        if (a[i] % 4 == 0)            count0++;Â
        // If mod gives 1        else if (a[i] % 4 == 1)            count1++;Â
        // If mod gives 2        else if (a[i] % 4 == 2)            count2++;Â
        // If mod gives 3        else if (a[i] % 4 == 3)            count3++;    }Â
    // Check the winning condition for X    if (count0 % 2 == 0        && count1 % 2 == 0        && count2 % 2 == 0        && count3 % 2 == 0)        return 1;    else        return 2;}Â
// Driver codeint 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
// Java implementation of the approachclass GFG{Â Â Â Â Â // Function to decide the winnerstatic int decideWinner(int []a, int n){Â Â Â Â int count0 = 0;Â Â Â Â int count1 = 0;Â Â Â Â int count2 = 0;Â Â Â Â int count3 = 0;Â
    // Iterate for all numbers in the array    for (int i = 0; i < n; i++)    {Â
        // Condition to countÂ
        // If mod gives 0        if (a[i] % 4 == 0)            count0++;Â
        // If mod gives 1        else if (a[i] % 4 == 1)            count1++;Â
        // If mod gives 2        else if (a[i] % 4 == 2)            count2++;Â
        // If mod gives 3        else if (a[i] % 4 == 3)            count3++;    }Â
    // Check the winning condition for X    if (count0 % 2 == 0 && count1 % 2 == 0 &&         count2 % 2 == 0 && count3 % 2 == 0)        return 1;    else        return 2;}Â
// Driver codepublic 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");}}Â
// This code is contributed by Akanksha Rai |
Python3
# Python3 implementation of the approachÂ
# Function to decide the winnerdef decideWinner(a, n):Â Â Â Â count0 = 0Â Â Â Â count1 = 0Â Â Â Â count2 = 0Â Â Â Â count3 = 0Â
    # Iterate for all numbers in the array    for i in range(n):Â
        # Condition to countÂ
        # If mod gives 0        if (a[i] % 4 == 0):            count0 += 1Â
        # If mod gives 1        elif (a[i] % 4 == 1):            count1 += 1Â
        # If mod gives 2        elif (a[i] % 4 == 2):            count2 += 1Â
        # If mod gives 3        elif (a[i] % 4 == 3):            count3 += 1         # Check the winning condition for X    if (count0 % 2 == 0 and count1 % 2 == 0 and        count2 % 2 == 0 and count3 % 2 == 0):        return 1    else:        return 2Â
# Driver codea = [4, 8, 5, 9]n = len(a)if (decideWinner(a, n) == 1):Â Â Â Â print("X wins")else:Â Â Â Â print("Y wins")Â
# This code is contributed by mohit kumar |
C#
// C# implementation of the approachusing System;class GFG{Â Â Â Â Â // Function to decide the winnerstatic int decideWinner(int []a, int n){Â Â Â Â int count0 = 0;Â Â Â Â int count1 = 0;Â Â Â Â int count2 = 0;Â Â Â Â int count3 = 0;Â
    // Iterate for all numbers in the array    for (int i = 0; i < n; i++)    {Â
        // Condition to countÂ
        // If mod gives 0        if (a[i] % 4 == 0)            count0++;Â
        // If mod gives 1        else if (a[i] % 4 == 1)            count1++;Â
        // If mod gives 2        else if (a[i] % 4 == 2)            count2++;Â
        // If mod gives 3        else if (a[i] % 4 == 3)            count3++;    }Â
    // Check the winning condition for X    if (count0 % 2 == 0 && count1 % 2 == 0 &&         count2 % 2 == 0 && count3 % 2 == 0)        return 1;    else        return 2;}Â
// Driver codepublic 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");}}Â
// This code is contributed by Akanksha Rai |
PHP
<?php// PHP implementation of the approach Â
// Function to decide the winner function decideWinner($a, $n) { Â Â Â Â $count0 = 0; Â Â Â Â $count1 = 0; Â Â Â Â $count2 = 0; Â Â Â Â $count3 = 0; Â
    // Iterate for all numbers in the array     for ($i = 0; $i < $n; $i++)     { Â
        // Condition to count Â
        // If mod gives 0         if ($a[$i] % 4 == 0)             $count0++; Â
        // If mod gives 1         else if ($a[$i] % 4 == 1)             $count1++; Â
        // If mod gives 2         else if ($a[$i] % 4 == 2)             $count2++; Â
        // If mod gives 3         else if ($a[$i] % 4 == 3)             $count3++;     } Â
    // Check the winning condition for X     if ($count0 % 2 == 0 && $count1 % 2 == 0 &&         $count2 % 2 == 0 && $count3 % 2 == 0)         return 1;     else        return 2; } Â
// Driver code$a = array( 4, 8, 5, 9 ); $n = count($a);Â
if (decideWinner($a, $n) == 1)     echo "X wins"; else    echo "Y wins";      // This code is contributed by Ryuga?> |
Javascript
<script>// javascript implementation of the approach   // Function to decide the winner    function decideWinner(a , n) {        var count0 = 0;        var count1 = 0;        var count2 = 0;        var count3 = 0;Â
        // Iterate for all numbers in the array        for (i = 0; i < n; i++) {Â
            // Condition to countÂ
            // If mod gives 0            if (a[i] % 4 == 0)                count0++;Â
            // If mod gives 1            else if (a[i] % 4 == 1)                count1++;Â
            // If mod gives 2            else if (a[i] % 4 == 2)                count2++;Â
            // If mod gives 3            else if (a[i] % 4 == 3)                count3++;        }Â
        // Check the winning condition for X        if (count0 % 2 == 0 && count1 % 2 == 0 && count2 % 2 == 0 && count3 % 2 == 0)            return 1;        else            return 2;    }Â
    // Driver code             var a = [ 4, 8, 5, 9 ];        var n = a.length;        if (decideWinner(a, n) == 1)            document.write("X wins");        else            document.write("Y wins");Â
// This code contributed by Rajput-Ji</script> |
X wins
Â
Time Complexity: O(n)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


