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
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++
#include <bits/stdc++.h>
using namespace std;
int maxTeams( int N1, int N2)
{
int count = 0;
while (N1 > 0 && N2 > 0 && N1 + N2 >= 3) {
if (N1 > N2) {
N1 -= 2;
N2 -= 1;
}
else {
N1 -= 1;
N2 -= 2;
}
count++;
}
return count;
}
int main()
{
int N1 = 4, N2 = 5;
cout << maxTeams(N1, N2);
return 0;
}
|
Java
class GFG
{
static int maxTeams( int N1, int N2)
{
int count = 0 ;
while (N1 > 0 && N2 > 0 && N1 + N2 >= 3 ) {
if (N1 > N2) {
N1 -= 2 ;
N2 -= 1 ;
}
else {
N1 -= 1 ;
N2 -= 2 ;
}
count++;
}
return count;
}
public static void main(String []args)
{
int N1 = 4 , N2 = 5 ;
System.out.println(maxTeams(N1, N2));
}
}
|
Python3
def maxTeams(N1, N2):
count = 0
while (N1 > 0 and N2 > 0 and N1 + N2 > = 3 ) :
if (N1 > N2):
N1 - = 2
N2 - = 1
else :
N1 - = 1
N2 - = 2
count = count + 1
return count
N1 = 4
N2 = 5
print (maxTeams(N1, N2))
|
C#
using System;
class GFG
{
static int maxTeams( int N1, int N2)
{
int count = 0;
while (N1 > 0 && N2 > 0 && N1 + N2 >= 3) {
if (N1 > N2) {
N1 -= 2;
N2 -= 1;
}
else {
N1 -= 1;
N2 -= 2;
}
count++;
}
return count;
}
public static void Main()
{
int N1 = 4, N2 = 5;
Console.WriteLine(maxTeams(N1, N2));
}
}
|
Javascript
<script>
function maxTeams(N1, N2)
{
let count = 0;
while (N1 > 0 && N2 > 0 && N1 + N2 >= 3) {
if (N1 > N2) {
N1 -= 2;
N2 -= 1;
}
else {
N1 -= 1;
N2 -= 2;
}
count++;
}
return count;
}
let N1 = 4, N2 = 5;
document.write(maxTeams(N1, N2));
</script>
|
PHP
<?php
function maxTeams( $N1 , $N2 )
{
$count = 0 ;
while ( $N1 > 0 && $N2 > 0 &&
$N1 + $N2 >= 3)
{
if ( $N1 > $N2 )
{
$N1 -= 2;
$N2 -= 1;
}
else
{
$N1 -= 1;
$N2 -= 2;
}
$count ++;
}
return $count ;
}
$N1 = 4 ;
$N2 = 5 ;
echo maxTeams( $N1 , $N2 );
?>
|
Time Complexity: O(min(N1,N2))
Auxiliary Space: O(1)
New Approach:- Here, another approach we can calculate the maximum number of teams that can be formed by dividing the total number of people from both groups by 3, rounding down to the nearest integer. This is because each team consists of 3 people.
However, we need to make sure that at least one person is chosen from both groups. So, if the number of people in one of the groups is less than 3, we cannot form a team and the answer would be 0. Otherwise, the answer would be the maximum number of teams that can be formed as calculated earlier.
Algorithm:-
- Calculate the total number of people by adding N1 and N2.
- Check if N1, N2, or the total number of people is less than 3. If so, it is impossible to form a team and the function returns 0.
- Calculate the maximum number of teams possible by dividing the total number of people by 3 (since each team must have 3 members).
- Return the maximum number of teams as the output of the function.
- In the driver code, the function is called with the values N1=4 and N2=5, and the result is printed.
Here’s the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int maxTeams( int N1, int N2) {
int total_people = N1 + N2;
if (N1 < 1 || N2 < 1 || total_people < 3) {
return 0;
}
int max_teams = total_people / 3;
return max_teams;
}
int main() {
int N1 = 4;
int N2 = 5;
cout << maxTeams(N1, N2) << endl;
return 0;
}
|
Java
public class Main {
static int maxTeams( int N1, int N2)
{
int totalPeople = N1 + N2;
if (N1 < 1 || N2 < 1 || totalPeople < 3 ) {
return 0 ;
}
int maxTeams = totalPeople / 3 ;
return maxTeams;
}
public static void main(String[] args)
{
int N1 = 4 ;
int N2 = 5 ;
System.out.println(maxTeams(N1, N2));
}
}
|
Python3
def maxTeams(N1, N2):
total_people = N1 + N2
if N1 < 1 or N2 < 1 or total_people < 3 :
return 0
max_teams = total_people / / 3
return max_teams
N1 = 4
N2 = 5
print (maxTeams(N1, N2))
|
C#
using System;
public class GFG
{
public static int MaxTeams( int N1, int N2)
{
int totalPeople = N1 + N2;
if (N1 < 1 || N2 < 1 || totalPeople < 3)
{
return 0;
}
int maxTeams = totalPeople / 3;
return maxTeams;
}
public static void Main()
{
int N1 = 4;
int N2 = 5;
Console.WriteLine(MaxTeams(N1, N2));
}
}
|
Javascript
function maxTeams(N1, N2) {
const totalPeople = N1 + N2;
if (N1 < 1 || N2 < 1 || totalPeople < 3) {
return 0;
}
const maxTeamsCount = Math.floor(totalPeople / 3);
return maxTeamsCount;
}
const N1 = 4;
const N2 = 5;
console.log(maxTeams(N1, N2));
|
Output:-
3
Time Complexity:- The time complexity of this code is O(1)
Auxiliary Space:- The auxiliary space complexity of this code is also O(1)
Last Updated :
04 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...