Prediga el ganador del juego sobre la base de la diferencia absoluta de la suma seleccionando números

Dada una array de N números. Dos jugadores X e Y juegan un juego donde en cada paso un jugador selecciona un número. Un número se puede seleccionar sólo una vez. Una vez seleccionados todos los números, el jugador X gana si la diferencia absoluta entre la suma de los números recogidos por X e Y es divisible por 4 , de lo contrario, gana  Y.
Nota: el jugador X comienza el juego y los números se seleccionan de manera óptima en cada paso.
Ejemplos: 
 

Entrada: a[] = {4, 8, 12, 16} 
Salida:
X elige 4 
Y elige 12 
X elige 8 
Y elige 16 
|(4 + 8) – (12 + 16)| = |12 – 28| = 16 que es divisible por 4. 
Por lo tanto, X gana
Entrada: a[] = {7, 9, 1} 
Salida:
 

Enfoque : Se pueden seguir los siguientes pasos para resolver el problema: 
 

  • Inicialice count0 , count1 , count2 y count3 a 0 .
  • Iterar para cada número en la array y aumentar los contadores anteriores en consecuencia si a[i] % 4 == 0 , a[i] % 4 == 1 , a[i] % 4 == 2 o a[i] % 4 == 3 .
  • Si count0 , count1 , count2 y count3 son todos números pares entonces X gana, de lo contrario Y ganará.

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to decide the winner
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 == 0)
        return 1;
    else
        return 2;
}
 
// Driver code
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

// Java implementation of the approach
class GFG
{
     
// Function to decide the winner
static 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 == 0)
        return 1;
    else
        return 2;
}
 
// Driver code
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");
}
}
 
// This code is contributed by Akanksha Rai

Python3

# Python3 implementation of the approach
 
# Function to decide the winner
def 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 == 0):
        return 1
    else:
        return 2
 
# Driver code
a = [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 approach
using System;
class GFG
{
     
// Function to decide the winner
static 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 == 0)
        return 1;
    else
        return 2;
}
 
// Driver code
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");
}
}
 
// 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 == 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 == 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>
Producción: 

X wins

 

Complejidad de tiempo: O(n)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por Striver y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *