Open In App

Maximum number of 3-person teams formed from two groups

Improve
Improve
Like Article
Like
Save
Share
Report

Given two integers N1 and N2 where, N1 is the number of people in group 1 and N2 is the number of people in group 2. The task is to count the maximum number of 3-person teams that can be formed when at least a single person is chosen from both the groups.
Examples: 
 

Input: N1 = 2, N2 = 8 
Output:
Team 1: 2 members from group 2 and 1 member from group 1 
Update: N1 = 1, N2 = 6 
Team 2: 2 members from group 2 and 1 member from group 1 
Update: N1 = 0, N2 = 4 
No further teams can be formed.
Input: N1 = 4, N2 = 5 
Output:

Approach: Choose a single person from the team with less members and choose 2 persons from the team with more members (while possible) and update count = count + 1. Print the count in the end.
Below is the implementation of the above approach: 
 

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


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>


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
?>


Output

3







Time Complexity: O(min(N1,N2))
Auxiliary Space: O(1)

New Approach:- Here, another approach we can calculate the maximum number of teams that can be formed by dividing the total number of people from both groups by 3, rounding down to the nearest integer. This is because each team consists of 3 people.

However, we need to make sure that at least one person is chosen from both groups. So, if the number of people in one of the groups is less than 3, we cannot form a team and the answer would be 0. Otherwise, the answer would be the maximum number of teams that can be formed as calculated earlier.

Algorithm:-

  • Calculate the total number of people by adding N1 and N2.
  • Check if N1, N2, or the total number of people is less than 3. If so, it is impossible to form a team and the function returns 0.
  • Calculate the maximum number of teams possible by dividing the total number of people by 3 (since each team must have 3 members).
  • Return the maximum number of teams as the output of the function.
  • In the driver code, the function is called with the values N1=4 and N2=5, and the result is printed.

Here’s the implementation of the above approach:

C++




#include <iostream>
using namespace std;
 
// Function to return the count of maximum teams possible
int maxTeams(int N1, int N2) {
    int total_people = N1 + N2;
    if (N1 < 1 || N2 < 1 || total_people < 3) {
        // If either N1 or N2 is less than 1, or the total number of people
        // is less than 3, it's not possible to form a team.
        return 0;
    }
 
    // Calculate the maximum number of teams that can be formed by dividing
    // the total number of people by 3 (each team requires 3 people).
    int max_teams = total_people / 3;
    return max_teams;
}
 
// Driver code
int main() {
    int N1 = 4;
    int N2 = 5;
    cout << maxTeams(N1, N2) << endl;
    return 0;
}


Java




public class Main {
    // Function to return the count of maximum teams
    // possible
    static int maxTeams(int N1, int N2)
    {
        int totalPeople = N1 + N2;
        if (N1 < 1 || N2 < 1 || totalPeople < 3) {
            // If either N1 or N2 is less than 1, or the
            // total number of people is less than 3, it's
            // not possible to form a team.
            return 0;
        }
 
        // Calculate the maximum number of teams that can be
        // formed by dividing the total number of people by
        // 3 (each team requires 3 people).
        int maxTeams = totalPeople / 3;
        return maxTeams;
    }
 
    public static void main(String[] args)
    {
        int N1 = 4;
        int N2 = 5;
        System.out.println(maxTeams(N1, N2));
    }
}


Python3




# Python3 implementation of the approach
 
# Function to return the count of maximum teams possible
 
 
def maxTeams(N1, N2):
    total_people = N1 + N2
    if N1 < 1 or N2 < 1 or total_people < 3:
        # Cannot form a team with less than 3 people or
        # if at least one group has no people
        return 0
    max_teams = total_people // 3
    return max_teams
 
 
# Driver code
N1 = 4
N2 = 5
print(maxTeams(N1, N2))


C#




using System;
 
public class GFG
{
    // Function to return the count of maximum teams possible
    public static int MaxTeams(int N1, int N2)
    {
        int totalPeople = N1 + N2;
        if (N1 < 1 || N2 < 1 || totalPeople < 3)
        {
            // If either N1 or N2 is less than 1, or the total number of people
            // is less than 3, it's not possible to form a team.
            return 0;
        }
 
        // Calculate the maximum number of teams that can be formed by dividing
        // the total number of people by 3 (each team requires 3 people).
        int maxTeams = totalPeople / 3;
        return maxTeams;
    }
 
    // Driver code
    public static void Main()
    {
        int N1 = 4;
        int N2 = 5;
        Console.WriteLine(MaxTeams(N1, N2));
    }
}


Javascript




// Function to return the count of maximum teams possible
function maxTeams(N1, N2) {
    const totalPeople = N1 + N2;
     
    // Check if either N1 or N2 is less than 1, or the total number of people
    // is less than 3, it's not possible to form a team.
    if (N1 < 1 || N2 < 1 || totalPeople < 3) {
        return 0;
    }
 
    // Calculate the maximum number of teams that can be formed by dividing
    // the total number of people by 3 (each team requires 3 people).
    const maxTeamsCount = Math.floor(totalPeople / 3);
    return maxTeamsCount;
}
 
// Driver code
const N1 = 4;
const N2 = 5;
console.log(maxTeams(N1, N2)); // Output: 3


Output:-

3

Time Complexity:- The time complexity of this code is O(1)
Auxiliary Space:- The auxiliary space complexity of this code is also O(1)



Last Updated : 04 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads