Número máximo de equipos de 3 personas formados a partir de dos grupos

Dados dos números enteros N1 y N2 , donde N1 es el número de personas en el grupo 1 y N2 es el número de personas en el grupo 2. La tarea es contar el número máximo de equipos de 3 personas que se pueden formar cuando al menos uno solo la persona es elegida de ambos grupos.
Ejemplos: 
 

Entrada: N1 = 2, N2 = 8 
Salida:
Equipo 1: 2 miembros del grupo 2 y 1 miembro del grupo 1 
Actualización: N1 = 1, N2 = 6 
Equipo 2: 2 miembros del grupo 2 y 1 miembro del grupo 1 
Actualización : N1 = 0, N2 = 4 
No se pueden formar más equipos.
Entrada: N1 = 4, N2 = 5 
Salida:

Enfoque: elija una sola persona del equipo con menos miembros y elija 2 personas del equipo con más miembros (siempre que sea posible) y actualice count = count + 1 . Imprime el conteo al final.
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 return the count
// of maximum teams possible
int maxTeams(int N1, int N2)
{
 
    int count = 0;
 
    // While it is possible to form a team
    while (N1 > 0 && N2 > 0 && N1 + N2 >= 3) {
 
        // Choose 2 members from group 1
        // and a single member from group 2
        if (N1 > N2) {
            N1 -= 2;
            N2 -= 1;
        }
 
        // Choose 2 members from group 2
        // and a single member from group 1
        else {
            N1 -= 1;
            N2 -= 2;
        }
 
        // Update the count
        count++;
    }
 
    // Return the count
    return count;
}
 
// Driver code
int main()
{
 
    int N1 = 4, N2 = 5;
    cout << maxTeams(N1, N2);
 
    return 0;
}

Java

// Java implementation of the approach
 
class GFG
{
    // Function to return the count
    // of maximum teams possible
    static int maxTeams(int N1, int N2)
    {
     
        int count = 0;
     
        // While it is possible to form a team
        while (N1 > 0 && N2 > 0 && N1 + N2 >= 3) {
     
            // Choose 2 members from group 1
            // and a single member from group 2
            if (N1 > N2) {
                N1 -= 2;
                N2 -= 1;
            }
     
            // Choose 2 members from group 2
            // and a single member from group 1
            else {
                N1 -= 1;
                N2 -= 2;
            }
     
            // Update the count
            count++;
        }
     
        // Return the count
        return count;
    }
     
    // Driver code
    public static void main(String []args)
    {
     
        int N1 = 4, N2 = 5;
        System.out.println(maxTeams(N1, N2));
     
         
    }
 
}
 
// This code is contributed by ihritik

Python3

# Python3 implementation of the approach
 
 
# Function to return the count
# of maximum teams possible
def maxTeams(N1, N2):
 
 
    count = 0
 
    # While it is possible to form a team
    while (N1 > 0 and N2 > 0 and N1 + N2 >= 3) :
 
        # Choose 2 members from group 1
        # and a single member from group 2
        if (N1 > N2):
            N1 -= 2
            N2 -= 1
         
 
        # Choose 2 members from group 2
        # and a single member from group 1
        else:
            N1 -= 1
            N2 -= 2
         
 
        # Update the count
        count=count+1
     
 
    # Return the count
    return count
 
     
# Driver code
N1 = 4
N2 = 5
print(maxTeams(N1, N2))
 
# This code is contributed by ihritik

C#

// C# implementation of the approach
 
using System;
class GFG
{
    // Function to return the count
    // of maximum teams possible
    static int maxTeams(int N1, int N2)
    {
     
        int count = 0;
     
        // While it is possible to form a team
        while (N1 > 0 && N2 > 0 && N1 + N2 >= 3) {
     
            // Choose 2 members from group 1
            // and a single member from group 2
            if (N1 > N2) {
                N1 -= 2;
                N2 -= 1;
            }
     
            // Choose 2 members from group 2
            // and a single member from group 1
            else {
                N1 -= 1;
                N2 -= 2;
            }
     
            // Update the count
            count++;
        }
     
        // Return the count
        return count;
    }
     
    // Driver code
    public static void Main()
    {
     
        int N1 = 4, N2 = 5;
        Console.WriteLine(maxTeams(N1, N2));
     
         
    }
 
}
 
// This code is contributed by ihritik

PHP

<?php
// PHP implementation of the approach
 
// Function to return the count
// of maximum teams possible
function maxTeams($N1, $N2)
{
    $count = 0 ;
 
    // While it is possible to form a team
    while ($N1 > 0 && $N2 > 0 &&
                $N1 + $N2 >= 3)
    {
 
        // Choose 2 members from group 1
        // and a single member from group 2
        if ($N1 > $N2)
        {
            $N1 -= 2;
            $N2 -= 1;
        }
 
        // Choose 2 members from group 2
        // and a single member from group 1
        else
        {
            $N1 -= 1;
            $N2 -= 2;
        }
 
        // Update the count
        $count++;
    }
 
    // Return the count
    return $count;
}
 
// Driver code
$N1 = 4 ;
$N2 = 5 ;
 
echo maxTeams($N1, $N2);
 
// This code is contributed by Ryuga
?>

Javascript

<script>
    // Javascript implementation of the approach
     
    // Function to return the count
    // of maximum teams possible
    function maxTeams(N1, N2)
    {
       
        let count = 0;
       
        // While it is possible to form a team
        while (N1 > 0 && N2 > 0 && N1 + N2 >= 3) {
       
            // Choose 2 members from group 1
            // and a single member from group 2
            if (N1 > N2) {
                N1 -= 2;
                N2 -= 1;
            }
       
            // Choose 2 members from group 2
            // and a single member from group 1
            else {
                N1 -= 1;
                N2 -= 2;
            }
       
            // Update the count
            count++;
        }
       
        // Return the count
        return count;
    }
     
    let N1 = 4, N2 = 5;
      document.write(maxTeams(N1, N2));
     
    // This code is contributed by divyeshrabadiya07.
</script>
Producción: 

3

 

Complejidad de tiempo: O(min(N1,N2))
Espacio auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por Vivek.Pandit 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 *