Maximum number of teams that can be formed with given persons - GeeksforGeeks

31 Mar, 2021
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++
Java
Python3
C#
PHP
Javascript
// 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 mbool 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 formedint 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 codeint main(){    int n = 4, m = 5;    cout << maxTeams(n, m);     return 0;}
// Java implementation of the approachclass 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
# Python 3 implementation of the approach # Function that returns true if it possible# to form a team with the given n and mdef 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 formeddef 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 codeif __name__ == '__main__':    n = 4    m = 5    print(maxTeams(n, m)) # This code is contributed by# Surendra_Gangwar
// C# implementation of the approachusing System; class GFG{     // Function that returns true if it possible// to form a team with the given n and mstatic 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 formedstatic 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 codepublic static void Main(){    int n = 4, m = 5;    Console.WriteLine(maxTeams(n, m));}} // This code is contributed by// tufan_gupta2000
<?php// PHP implementation of the approach // Function that returns true if it possible// to form a team with the given n and mfunction canFormTeam($n, $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 formedfunction 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?>
<script>// javascript implementation of the approach    // Function that returns true     // if it possible to form a    // team with the given n and m    function canFormTeam(n, 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    function maxTeams(n , m)    {             // To store the required count        // of teams formed        var 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    var n = 4, m = 5;    document.write(maxTeams(n, m)); // This code is contributed by todaysgaurav</script>
3
 
tufan_gupta2000
ankthon
SURENDRA_GANGWAR
Mithun Kumar
todaysgaurav
Competitive Programming
Greedy
Mathematical
Greedy
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Prefix Sum Array - Implementation and Applications in Competitive Programming
Ordered Set and GNU C++ PBDS
Modulo 10^9+7 (1000000007)
Bits manipulation (Important tactics)
7 Best Coding Challenge Websites in 2020
Dijkstra's shortest path algorithm | Greedy Algo-7
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Program for array rotation
Write a program to print all permutations of a given string