给定项目A的n个实例和项目B的m个实例。找到可以使用这些项目形成的大小为3的组的最大数目,以使所有组都包含这两种类型的项目,即,一个组不应该具有以下所有项目类型A或所有类型B的项目。
形成的组中类型A的项目总数必须小于或等于n。
形成的组中类型B的项目总数必须小于或等于m。
例子 :
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.
观察:
1.如果m> = 2n,将有n个组。如果n> = 2m,则可能有m个组。
2.假设n = 3和m = 3,所以项目A的一个实例将与项目B的两个实例组成一个组,而项目B的一个实例将与项目A的两个实例组成一个组。因此,最多两个组是可能的。因此,通过将m和m除以3,找到给定n和m的此类条件的总数。此后,可以保留每种类型的0、1、2个实例。为了找到左侧实例的组数:
a)如果n = 0或m = 0,则0组是可能的。
b)如果n + m> = 3,则只能有1个组。
解决此问题的算法:
1.如果n> = 2m,则最大组数= n。
2.否则,如果m> = 2n,则最大组数= m。
3.否则,如果(m + n)%3 == 0,则最大组数=(m + n)/ 3;
4.其他最大组数=(n + m)/ 3。并设置n = n%3和m = m%3。
a)如果n!= 0 && m!= 0 &&(n + m)> = 3,则将最大组数加1。
下面是上述想法的实现:
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
输出 :
3