# Maximum number of 3-person teams formed from two groups

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: 2
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: 3

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `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 memebers from group 1 ` `        ``// and a single memeber from group 2 ` `        ``if` `(N1 > N2) { ` `            ``N1 -= 2; ` `            ``N2 -= 1; ` `        ``} ` ` `  `        ``// Choose 2 memebers from group 2 ` `        ``// and a single memeber 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 memebers from group 1 ` `            ``// and a single memeber from group 2 ` `            ``if` `(N1 > N2) { ` `                ``N1 -= ``2``; ` `                ``N2 -= ``1``; ` `            ``} ` `     `  `            ``// Choose 2 memebers from group 2 ` `            ``// and a single memeber 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 memebers from group 1 ` `        ``# and a single memeber from group 2 ` `        ``if` `(N1 > N2):  ` `            ``N1 ``-``=` `2` `            ``N2 ``-``=` `1` `         `  ` `  `        ``# Choose 2 memebers from group 2 ` `        ``# and a single memeber 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 memebers from group 1 ` `            ``// and a single memeber from group 2 ` `            ``if` `(N1 > N2) { ` `                ``N1 -= 2; ` `                ``N2 -= 1; ` `            ``} ` `     `  `            ``// Choose 2 memebers from group 2 ` `            ``// and a single memeber 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

 ` 0 && ``\$N2` `> 0 && ` `                ``\$N1` `+ ``\$N2` `>= 3) ` `    ``{  ` ` `  `        ``// Choose 2 memebers from group 1  ` `        ``// and a single memeber from group 2  ` `        ``if` `(``\$N1` `> ``\$N2``)  ` `        ``{  ` `            ``\$N1` `-= 2;  ` `            ``\$N2` `-= 1;  ` `        ``}  ` ` `  `        ``// Choose 2 memebers from group 2  ` `        ``// and a single memeber 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
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : ihritik, AnkitRai01

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.