Find the Number of Ways to Pair People using C++

C++Server Side ProgrammingProgramming

To solve a problem in which n − the number of people now each person can either be single or be present in a pair, so we need to find the total number of ways these people can be paired up.

Input : 3

Output: 4

Explanation : [ {1}, {2}, {3},], [{1, 2}, {3}], [{1}, {2, 3}], [{1, 3}, {2}] these four ways are the only ways we can pa up these 3 people.

Input : 6

Output : 76

Approach to Find the Solution

In this approach, we are going to use the formula of Young Tableau to calculate this problem and the formula that we are going to use is −

A[n] = A[n-1] + (n-1) * A[n-2]


C++ Code for the Above Approach

#include <bits/stdc++.h>
using namespace std;
int Young_Tableau(int n){
    int A[n + 1];// To store the answer.
    A[1] = 1; // initial values
    A[2] = 2; // initial values
    for (int i = 3; i <= n; i++) { // using the formula of "Young Tableau" to calculate our answer
        A[i] = A[i - 1] + (i - 1) * A[i - 2];
    return A[n]; // returning the answer
int main(){
    int n = 6;
    cout << Young_Tableau(n);
    return 0;



Explanation of the Above Code

In the above approach, we simply apply the formula for Young Tableau, where we need to find the previous two numbers. Now we can store these numbers in array indices. By subscribing, we can attain their values for our formula, and hence we calculate our answer.


In this tutorial, we solve a problem to find several ways to pair people. We also learned the C++ program for this problem and the complete approach (Normal) by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this tutorial helpful.

Published on 25-Nov-2021 09:20:11