Maximum number of teams that can be formed with given persons
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: 2
(Type1, Type2, Type2) and (Type1, Type2, Type2) are the two possible teams.
Input: N = 4, M = 5
Output: 3
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++
#include <bits/stdc++.h>
using namespace std;
bool canFormTeam( int n, int m)
{
if (n >= 1 && m >= 2)
return true ;
if (m >= 1 && n >= 2)
return true ;
return false ;
}
int maxTeams( int n, int m)
{
int count = 0;
while (canFormTeam(n, m)) {
if (n > m) {
n -= 2;
m -= 1;
}
else {
m -= 2;
n -= 1;
}
count++;
}
return count;
}
int main()
{
int n = 4, m = 5;
cout << maxTeams(n, m);
return 0;
}
|
Java
class GFG
{
static boolean canFormTeam( int n, int m)
{
if (n >= 1 && m >= 2 )
return true ;
if (m >= 1 && n >= 2 )
return true ;
return false ;
}
static int maxTeams( int n, int m)
{
int count = 0 ;
while (canFormTeam(n, m))
{
if (n > m)
{
n -= 2 ;
m -= 1 ;
}
else
{
m -= 2 ;
n -= 1 ;
}
count++;
}
return count;
}
public static void main(String args[])
{
int n = 4 , m = 5 ;
System.out.println(maxTeams(n, m));
}
}
|
Python3
def canFormTeam(n, m):
if (n > = 1 and m > = 2 ):
return True
if (m > = 1 and n > = 2 ):
return True
return False
def maxTeams(n, m):
count = 0
while (canFormTeam(n, m)):
if (n > m):
n - = 2
m - = 1
else :
m - = 2
n - = 1
count + = 1
return count
if __name__ = = '__main__' :
n = 4
m = 5
print (maxTeams(n, m))
|
C#
using System;
class GFG
{
static bool canFormTeam( int n, int m)
{
if (n >= 1 && m >= 2)
return true ;
if (m >= 1 && n >= 2)
return true ;
return false ;
}
static int maxTeams( int n, int m)
{
int count = 0;
while (canFormTeam(n, m))
{
if (n > m)
{
n -= 2;
m -= 1;
}
else
{
m -= 2;
n -= 1;
}
count++;
}
return count;
}
public static void Main()
{
int n = 4, m = 5;
Console.WriteLine(maxTeams(n, m));
}
}
|
PHP
<?php
function canFormTeam( $n , $m )
{
if ( $n >= 1 && $m >= 2)
return true;
if ( $m >= 1 && $n >= 2)
return true;
return false;
}
function maxTeams( $n , $m )
{
$count = 0;
while (canFormTeam( $n , $m ))
{
if ( $n > $m )
{
$n -= 2;
$m -= 1;
}
else
{
$m -= 2;
$n -= 1;
}
$count ++;
}
return $count ;
}
$n = 4;
$m = 5;
echo maxTeams( $n , $m );
?>
|
Javascript
<script>
function canFormTeam(n, m)
{
if (n >= 1 && m >= 2)
return true ;
if (m >= 1 && n >= 2)
return true ;
return false ;
}
function maxTeams(n , m)
{
var count = 0;
while (canFormTeam(n, m))
{
if (n > m)
{
n -= 2;
m -= 1;
}
else
{
m -= 2;
n -= 1;
}
count++;
}
return count;
}
var n = 4, m = 5;
document.write(maxTeams(n, m));
</script>
|
Time Complexity: O(min(n, m))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...