
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum number of 3-person teams formed from two groups in C++
In this problem, we are given two integers N and M, N is the number of people in group 1, and M is the number of people in group 2. Our task is to create a program to find the maximum number of 3-person teams formed from two groups.
We will be creating teams of 3 people by selecting a person from these groups such that the maximum teams can be made. Each team must have at least one person from each group.
Let’s take an example to understand the problem,
Input − N = 5, M = 3
Output − 2
Explanation −
The teams will be as follows −
Team 1: Group 1 Member->2 ; Group 2 Member->1 Left in Group 1 = 3 ; left in Group 2 = 2 Team 2: Group 1 Member->2 ; Group 2 Member->1 Left in Group 1 = 1 ; left in Group 2 = 1 No more teams of 3 can be formed.
To solve this problem, we will create teams taking 1 member form group with fewer persons and 2 members from the other group. And update the count of persons in each group. Also, we will maintain a count of teams and increment it after each team creation until the team creation is possible.
Example
Program to find the maximum number of 3-person teams formed from two groups −
#include <iostream> using namespace std; int CountTeams(int N, int M) { int teamCount = 0; while (N >= 1 && M >= 1 && N + M >= 3) { if (N > M) { N = N-2; M = M-1; } else { N = N-1; M = M-2; } teamCount++; } return teamCount; } int main() { int N = 5, M = 3; cout<<"The maximum number of 3-person teams is "<<CountTeams(N, M); return 0; }
Output
The maximum number of 3-person teams is 2
- Related Articles
- Maximum number of groups of size 3 containing two type of items in C++
- Number of groups of magnets formed from N magnets in C++
- C++ program to find number of groups can be formed from set of programmers
- Number of Groups of Sizes Two Or Three Divisible By 3 in C++
- Minimum and Maximum number of pairs in m teams of n people in C++
- C++ Program to count number of teams can be formed for coding challenge
- Count Number of Teams in C++
- Finding maximum number from two arrays in JavaScript
- Program to find maximum number of balanced groups of parentheses in Python
- Program to find maximum number of groups getting fresh donuts in Python
- Maximum factors formed by two numbers in Python
- Find maximum number that can be formed using digits of a given number in C++
- Return the difference between the maximum & minimum number formed out of the number n in JavaScript
- What is the minimum and maximum number of digits in the sum if we add any two 3 digit number
- Program to find maximum number of K-sized groups with distinct type items are possible in Python
