Maximum number of teams that can be formed with given persons

Given two integers N and M which denote the number of persons of Type1 and Type2 respectively. The task is to find the maximum number of teams that can be formed with these two types of persons. A team can contain either 2 persons of Type1 and 1 person of Type2 or 1 person of Type1 and 2 persons of Type2.

Examples:

Input: N = 2, M = 6
Output: 2
(Type1, Type2, Type2) and (Type1, Type2, Type2) are the two possible teams.

Input: N = 4, M = 5
Output: 3



Approach: A greedy approach is to choose 2 persons from the group which has more members and 1 person from the group with lesser members and update the count of persons in each of the group accordingly. Termination condition will be when no more teams can be formed.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that returns true if it possible
// to form a team with the given n and m
bool canFormTeam(int n, int m)
{
  
    // 1 person of Type1 and 2 persons of Type2
    // can be chosen
    if (n >= 1 && m >= 2)
        return true;
  
    // 1 person of Type2 and 2 persons of Type1
    // can be chosen
    if (m >= 1 && n >= 2)
        return true;
  
    // Cannot from a team
    return false;
}
  
// Function to return the maximum number of teams
// that can be formed
int maxTeams(int n, int m)
{
    // To store the required count of teams formed
    int count = 0;
  
    while (canFormTeam(n, m)) {
        if (n > m) {
  
            // Choose 2 persons of Type1
            n -= 2;
  
            // And 1 person of Type2
            m -= 1;
        }
        else {
  
            // Choose 2 persons of Type2
            m -= 2;
  
            // And 1 person of Type1
            n -= 1;
        }
  
        // Another team has been formed
        count++;
    }
  
    return count;
}
  
// Driver code
int main()
{
    int n = 4, m = 5;
    cout << maxTeams(n, m);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class GFG 
          
    // Function that returns true 
    // if it possible to form a 
    // team with the given n and m 
    static boolean canFormTeam(int n, int m) 
    
      
        // 1 person of Type1 and 2 persons 
        // of Type2 can be chosen 
        if (n >= 1 && m >= 2
            return true
      
        // 1 person of Type2 and 2 persons 
        // of Type1 can be chosen 
        if (m >= 1 && n >= 2
            return true
      
        // Cannot from a team 
        return false
    
      
    // Function to return the maximum 
    // number of teams that can be formed 
    static int maxTeams(int n, int m) 
    
        // To store the required count 
        // of teams formed 
        int count = 0
      
        while (canFormTeam(n, m)) 
        
            if (n > m) 
            
      
                // Choose 2 persons of Type1 
                n -= 2
      
                // And 1 person of Type2 
                m -= 1
            
            else
            
      
                // Choose 2 persons of Type2 
                m -= 2
      
                // And 1 person of Type1 
                n -= 1
            
      
            // Another team has been formed 
            count++; 
        
        return count; 
    
  
    // Driver code 
    public static void main(String args[]) 
    
        int n = 4, m = 5
        System.out.println(maxTeams(n, m)); 
    
  
// This code is contributed by Ryuga

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of the approach
  
# Function that returns true if it possible
# to form a team with the given n and m
def canFormTeam(n, m):
  
    # 1 person of Type1 and 2 persons of Type2
    # can be chosen
    if (n >= 1 and m >= 2):
        return True
  
    # 1 person of Type2 and 2 persons of Type1
    # can be chosen
    if (m >= 1 and n >= 2):
        return True
  
    # Cannot from a team
    return False
  
# Function to return the maximum number of teams
# that can be formed
def maxTeams(n, m):
    # To store the required count of teams formed
    count = 0
  
    while (canFormTeam(n, m)):
        if (n > m):
            # Choose 2 persons of Type1
            n -= 2
  
            # And 1 person of Type2
            m -= 1
        
        else:
            # Choose 2 persons of Type2
            m -= 2
  
            # And 1 person of Type1
            n -= 1
  
        # Another team has been formed
        count += 1
  
    return count
  
# Driver code
if __name__ == '__main__':
    n = 4
    m = 5
    print(maxTeams(n, m))
  
# This code is contributed by
# Surendra_Gangwar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
      
// Function that returns true if it possible
// to form a team with the given n and m
static bool canFormTeam(int n, int m)
{
  
    // 1 person of Type1 and 2 persons
    //  of Type2 can be chosen
    if (n >= 1 && m >= 2)
        return true;
  
    // 1 person of Type2 and 2 persons 
    // of Type1 can be chosen
    if (m >= 1 && n >= 2)
        return true;
  
    // Cannot from a team
    return false;
}
  
// Function to return the maximum 
// number of teams that can be formed
static int maxTeams(int n, int m)
{
    // To store the required count
    // of teams formed
    int count = 0;
  
    while (canFormTeam(n, m))
    {
        if (n > m) 
        {
  
            // Choose 2 persons of Type1
            n -= 2;
  
            // And 1 person of Type2
            m -= 1;
        }
        else
        {
  
            // Choose 2 persons of Type2
            m -= 2;
  
            // And 1 person of Type1
            n -= 1;
        }
  
        // Another team has been formed
        count++;
    }
    return count;
}
  
// Driver code
public static void Main()
{
    int n = 4, m = 5;
    Console.WriteLine(maxTeams(n, m));
}
}
  
// This code is contributed by 
// tufan_gupta2000

chevron_right


PHP

= 1 && $m >= 2)
return true;

// 1 person of Type2 and 2 persons
// of Type1 can be chosen
if ($m >= 1 && $n >= 2)
return true;

// Cannot from a team
return false;
}

// Function to return the maximum number
// of teams that can be formed
function maxTeams($n, $m)
{

// To store the required count
// of teams formed
$count = 0;

while (canFormTeam($n, $m))
{
if ($n > $m)
{

// Choose 2 persons of Type1
$n -= 2;

// And 1 person of Type2
$m -= 1;
}
else
{

// Choose 2 persons of Type2
$m -= 2;

// And 1 person of Type1
$n -= 1;
}

// Another team has been formed
$count++;
}

return $count;
}

// Driver code
$n = 4;
$m = 5;
echo maxTeams($n, $m);

// This code is contributed by mits
?>

Output:

3


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.