# Maximum number of groups of size 3 containing two type of items

• Difficulty Level : Easy
• Last Updated : 20 Aug, 2021

Given n instance of item A and m instance of item B. Find the maximum number of groups of size 3 that can be formed using these items such that all groups contain items of both types, i.e., a group should not have either all items of type A or all items of type B.
Total number of items of type A in the formed groups must be less than or equal to n.
Total number of items of type B in the formed groups must be less than or equal to m.
Examples :

```Input : n = 2 and m = 6.
Output : 2
In group 1, one item of type A and two items of
type B. Similarly, in the group 2, one item of
type A and two items of type B.
We have used 2 (<= n) items of type A and 4 (<= m)
items of type B.

Input : n = 4 and m = 5.
Output : 3
In group 1, one item of type A and two items of type B.
In group 2, one item of type B and two items of type A.
In group 3, one item of type A and two items of type B.
We have used 4 (<= n) items of type A and 5 (<= 5)
items of type B.```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Observation:
1. There will be n groups possible if m >= 2n. Or there will be m groups possible, if n >= 2m.
2. Suppose n = 3 and m = 3, so one instance of item A will make a group with the two instance of item B and one instance of item B will make a group with the two instance of item A. So, maximum two groups are possible. So find the total number of such conditions with given n and m by dividing m and m by 3. After this, there can be 0, 1, 2 instances of each type can be left. For finding the number of groups for the left instances:
a) If n = 0 or m = 0, 0 group is possible.
b) If n + m >= 3, only 1 group is possible.
Algorithm for solving this problem:
1. If n >= 2m, maximum number of groups = n.
2. Else if m >= 2n, maximum number of groups = m.
3. Else If (m + n) % 3 == 0, maximum number of group = (m + n)/3;
4. Else maximum number of group = (n + m)/3. And set n = n%3 and m = m%3.
a) If n != 0 && m != 0 && (n + m) >= 3, add one to maximum number of groups.
Below is implementation of the above idea :

## C++

 `// C++ program to calculate``// maximum number of groups``#include` `using` `namespace` `std;` `// Implements above mentioned steps.``int` `maxGroup(``int` `n, ``int` `m)``{``    ``if` `(n >= 2 * m)``        ``return` `n;``    ``if` `(m >= 2 * n)``        ``return` `m;``    ``if` `((m + n) % 3 == 0)``        ``return` `(m + n)/3;` `    ``int` `ans = (m + n)/3;``    ``m %= 3;``    ``n %= 3;` `    ``if` `(m && n && (m + n) >= 3)``        ``ans++;` `    ``return` `ans;``}` `// Driver code``int` `main()``{``    ``int` `n = 4, m = 5;``    ``cout << maxGroup(n, m) << endl;``    ``return` `0;``}`

## Java

 `// Java program to calculate``// maximum number of groups``import` `java.io.*;` `public` `class` `GFG{``    ` `// Implements above mentioned steps.``static` `int` `maxGroup(``int` `n, ``int` `m)``{``    ``if` `(n >= ``2` `* m)``        ``return` `n;``    ``if` `(m >= ``2` `* n)``        ``return` `m;``    ``if` `((m + n) % ``3` `== ``0``)``        ``return` `(m + n) / ``3``;` `    ``int` `ans = (m + n) / ``3``;``    ``m %= ``3``;``    ``n %= ``3``;` `    ``if` `(m > ``0` `&& n > ``0` `&& (m + n) >= ``3``)``        ``ans++;` `    ``return` `ans;``}` `    ``// Driver code``    ``static` `public` `void` `main (String[] args)``    ``{``            ``int` `n = ``4``, m = ``5``;``    ``System.out.println(maxGroup(n, m));``    ``}``}` `// This code is contributed by vt_m.`

## Python3

 `# Python3 program to calculate maximum``# number of groups` `# Implements above mentioned steps``def` `maxGroup(n, m):``    ` `    ``if` `n >``=` `2` `*` `m:``        ``return` `n``    ``if` `m >``=` `2` `*` `n:``        ``return` `m``    ``if` `(m ``+` `n) ``%` `3` `=``=` `0``:``        ``return` `(m ``+` `n) ``/``/` `3``    ``ans ``=` `(m ``+` `n) ``/``/` `3``    ``m ``=` `m ``%` `3``    ``n ``=` `n ``%` `3``    ` `    ``if` `m ``and` `n ``and` `(m ``+` `n) >``=` `3``:``        ``ans ``+``=` `1``    ``return` `ans``    ` `# Driver Code``n, m ``=` `4``, ``5``print``(maxGroup(n, m))` `# This code is contributed``# by Mohit kumar 29`

## C#

 `// C# program to calculate``// maximum number of groups``using` `System;` `public` `class` `GFG{``    ` `// Implements above mentioned steps.``static` `int` `maxGroup(``int` `n, ``int` `m)``{``    ``if` `(n >= 2 * m)``        ``return` `n;``    ``if` `(m >= 2 * n)``        ``return` `m;``    ``if` `((m + n) % 3 == 0)``        ``return` `(m + n) / 3;` `    ``int` `ans = (m + n) / 3;``    ``m %= 3;``    ``n %= 3;` `    ``if` `(m > 0 && n > 0 && (m + n) >= 3)``        ``ans++;` `    ``return` `ans;``}` `    ``// Driver code``    ``static` `public` `void` `Main ()``    ``{``        ``int` `n = 4, m = 5;``        ``Console.WriteLine(maxGroup(n, m));``    ``}``}` `// This code is contributed by vt_m.`

## PHP

 `= 2 * ``\$m``)``        ``return` `n;``    ``if` `(``\$m` `>= 2 * ``\$n``)``        ``return` `m;``    ``if` `(((``\$m` `+ ``\$n``) % 3) == 0)``        ``return` `(``\$m` `+ ``\$n``) / 3;` `    ``\$ans` `= (``\$m` `+ ``\$n``) / 3;``    ``\$m` `%= 3;``    ``\$n` `%= 3;` `    ``if` `(``\$m` `&& ``\$n` `&& (``\$m` `+ ``\$n``) >= 3)``        ``\$ans``++;` `    ``return` `\$ans``;``}` `// Driver code``\$n` `= 4; ``\$m` `= 5;``echo` `maxGroup(``\$n``, ``\$m``) ;` `// This code is contributed``// by nitin mittal.``?>`

## Javascript

 ``

Output :

`3`

Time Complexity: O(1)
Auxiliary Space: O(1)
This article is contributed by Anuj Chauhan(anuj0503). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.