
- 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
Minimum and Maximum number of pairs in m teams of n people in C++
Problem statement
N participants of the competition were split into M teams in some manner so that each team has at least one participant. After the competition each pair of participants from the same team became friends.
Your task is to write a program that will find the minimum and the maximum number of pairs of friends that could have formed by the end of the competition.
Algorithm
1. We can obtain max pairs using below formula: maxPairs = ((n – m) * (n – m + 1)) / 2 2. We can obtain min pairs using below formula: minPairs = m * (((n - m) / m + 1) * ((n - m) / m)) / 2 + ceil((n - m) / double(m)) * ((n - m) % m);
Example
#include <iostream> #include <cmath> using namespace std; void getPairs(int n, int m){ int maxPairs = ((n - m + 1) * (n - m)) / 2; int minPairs = m * (((n - m) / m + 1) * ((n - m) / m)) / 2 + ceil((n - m) / double(m)) * ((n - m) % m); cout << "Minimum pairs = " << minPairs << "\n"; cout << "Maximum pairs = " << maxPairs << "\n"; } int main(){ getPairs(3, 2); return 0; }
Output
When you compile and execute the above program. It generates the following output−
Minimum pairs = 1 Maximum pairs = 1
- Related Articles
- Maximum and minimum of an array using minimum number of comparisons in C
- Maximum number of 3-person teams formed from two groups in C++
- Number of pairs with maximum sum in C++
- Convert a number m to n using minimum number of given operations in C++
- Count Number of Teams in C++
- Find the minimum number of steps to reach M from N in C++
- Finding minimum number of required operations to reach n from m in JavaScript
- Find a positive number M such that gcd(N^M,N&M) is maximum in Python
- Program to find minimum number of people to teach in Python
- Return the difference between the maximum & minimum number formed out of the number n in JavaScript
- C++ Program to find pairs of sequences where sequence holds minimum and maximum elements
- Maximum number of people that can be killed with strength P in C++
- Program to find maximum number of people we can make happy in Python
- Maximum number of pieces in N cuts in C++
- Maximum Length Chain of Pairs in C++

Advertisements