
- 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
Counting pairs when a person can form pair with at most one in C++
We are given with N no. of participants in a coding competition. The goal is to find the no. of pairs that are possible when a person can pair with at most one other person. So a pair has at most 2 participants. The participants are allowed to take part alone also.
We can solve this using recurrence where pairs=
count=1 when n=0 or 1 ( only one person left )
if person remains single n reduced to n-1
now for remaining pairing people left = n-2
count=makePairs(p-1) + (p-1)*makePairs(p-2);
Let’s understand with examples.
Input − persons=3
Output − Count of ways to make pair − 4
Explanation −
If three persons are a,b,c then ways of pairing could be: (a,b), (c) → c remained single (a,c), (b) → b remained single (b,c), (a) → a remained single (a),(b),(c) → all remained single Total ways = 4
Input − persons=2
Output − Count of ways to make pair − 2
Explanation −
I persons are a,b then ways of pairing could be − (a,b) → both paired (a),(b) → both remained single Total ways = 2
Approach used in the below program is as follows
We take an integer person to store the number of participants.
Function makePairs(int p) takes no. of persons as input and returns the count of ways in which they can pair themselves.
Take the initial count as 0.
If p=0 or 1 then the only way is 1 to remain single.
Else person can chose to remain single, then remaining (p-1) will find pairs using (p1)*makePairs(p-2).
Final value of count is returned as no. of ways of pairing at the end.
Example
#include<iostream> using namespace std; int makePairs(int p){ int count=0; // Base condition if (p==0 || p==1) { count=1; } else { count=makePairs(p-1) + (p-1)*makePairs(p-2); } return count; } int main(){ int persons = 5; cout <<"Number of ways to make pair ( or remain single ):"<<makePairs(persons); return 0; }
Output
If we run the above code it will generate the following output −
Number of ways to make pair ( or remain single ): 26
- Related Articles
- Counting pairs with range sum in a limit in JavaScript
- Form the smallest number using at most one swap operation in C++
- Form the largest number using at most one swap operation C++
- Can two right angles form a linear pair?
- Counting adjacent pairs of words in JavaScript
- Find the maximum cost of an array of pairs choosing at most K pairs in C++
- Forming palindrome using at most one deletion in JavaScript
- Match any string containing at most one p.
- Maximum sum subarray removing at most one element in C++
- What was the time when you felt a person was actually a shrewd one?
- At which trophic level a person is feeding when he is eating:roasted chickenbreadeggsapplefish
- Explain clearly why, a person who has lost the sight of one eye is at a disadvantage compared with the normal person who has two good eyes.
- Finding the maximum number using at most one swap in JavaScript
- Smallest number formed by shuffling one digit at most in JavaScript
- What happens when HIV infects a person?
