 Open in App
Not now

# Maximum number of teams that can be formed with given persons

• Difficulty Level : Medium
• Last Updated : 23 Jun, 2022

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:
(Type1, Type2, Type2) and (Type1, Type2, Type2) are the two possible teams.
Input: N = 4, M = 5
Output:

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

 `// C++ implementation of the approach` `#include ` `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;` `}`

## Java

 `// 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`

## Python3

 `# 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`

## C#

 `// 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`

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

## Javascript

 ``

Output:

`3`

Time Complexity: O(min(n, m))

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles