- Related Questions & Answers
- Find the maximum cost of an array of pairs choosing at most K pairs in C++
- Maximum sum subarray removing at most one element in C++
- Match any string containing at most one p.
- What was the time when you felt a person was actually a shrewd one?
- What makes a person a champion at anything?
- Maximize the maximum subarray sum after removing at most one element in C++
- Longest Substring with At Most K Distinct Characters in C++
- Longest Substring with At Most Two Distinct Characters in C++
- Who is the most accidentally famous person ever?
- Can a person die due to Anaemia?
- Numbers At Most N Given Digit Set in C++
- Print all numbers less than N with at-most 2 unique digits in C++
- Maximum average of a subarray of size of at least X and at most Y in C++
- Displaying at most 10 characters in a string in Java
- Find a pair with the given difference in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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.

#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; }

If we run the above code it will generate the following output −

Number of ways to make pair ( or remain single ): 26

Advertisements