给定两个整数N1和N2 ,其中N1是第1组的人数, N2是第2组的人数。任务是计算至少一个以上时可以组建的3人团队的最大数量。从两个组中都选择一个人。
例子:
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
方法:从成员较少的团队中选择一个人,然后从成员较多的团队中选择2个人(如果可能),并更新count = count + 1 。最后打印计数。
下面是上述方法的实现:
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
?>
输出:
3
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。