Friends Pairing Problem in C++


In this problem, we are given a positive integer N denoting the number of friends in a group. Our task is to create a program to solve the Friends Pairing Problem.

Each friend of the group can either remain single or can pair up with one other friend. The pairing of each friend of the group can be done only once.

Let’s take an example to understand the problem

Input: n = 3
Output: 4
Explanation:
Let’s say the 3 members of the group are A, B and C.
The pairing can be done as :
{A}, {B}, {C}
{A, B}, {C}
{A, C}, {B}
{A}, {B, C}

Solution Approach

One method to solve the problem is to find a general formula to get all possible pairing for the n students of the group.

Let’s say we have n friends in the group. And the ways these friends can pair up is f(n).

Each friend of the group can either stay single or pair up with another friend in the group. Let’s see the output in each case.

  • Case 1 − Nth friend chooses to stay single, one member from group is reduced, the next recursion will be from (N-1) i.e. f(N-1).

  • Case 2 − Nth friend chooses to pair up with another member, (N-2) member remain and the next recursion will be from N-2. Recursively called as f(N) = f(N-1) + (N-1) * f(N-2)

Their are multiple ways be can solve this.

Example

Program to illustrate the working of our solution

#include <iostream>
using namespace std;

int countGroupPairing(int N){

   int dpArr[N + 1];
   for (int i = 0; i <= N; i++) {
      if (i <= 2)
         dpArr[i] = i;
      else
         dpArr[i] = dpArr[i - 1] + (i - 1) * dpArr[i - 2];
   }
   return dpArr[N];
}
int main(){

   int N = 6;
   cout<<"The number of friends in the group is "<<N<<endl;
   cout<<"The total number of possible pairs is "<<countGroupPairing(N);
   return 0;
}

Output

The number of friends in the group is 6
The total number of possible pairs is 76

Recursive approach to implement the solution

Example

Program to illustrate the working of our solution

#include <bits/stdc++.h>
using namespace std;
int dpArr[1000];

int countGroupPairing(int N){

   memset(dpArr, -1, sizeof(dpArr));
   if (dpArr[N] != -1)
      return dpArr[N];
   if (N > 2)
      return dpArr[N] = countGroupPairing(N - 1) + (N - 1) * countGroupPairing(N - 2);
   else
      return dpArr[N] = N;
}
int main(){

   int N = 6;
   cout<<"The number of friends in the group is "<<N<<endl;
   cout<<"The total number of possible pairs is "<<countGroupPairing(N);
   return 0;
}

Output

The number of friends in the group is 6
The total number of possible pairs is 76

One more method to solve the problem, is by optimising the fibonacci series to fit in the given solution

Example

Program to illustrate the working of our solution

#include <bits/stdc++.h>
using namespace std;
int dpArr[1000];

int countGroupPairing(int N){

   int val1 = 1, val2 = 2, val3 = 0;
   if (N <= 2) {
      return N;
   }
   for (int i = 3; i <= N; i++) {
      val3 = val2 + (i - 1) * val1;
      val1 = val2;
      val2 = val3;
   }
   return val3;
}
int main(){

   int N = 6;
   cout<<"The number of friends in the group is "<<N<<endl;
   cout<<"The total number of possible pairs is "<<countGroupPairing(N);
   return 0;
}

Output

The number of friends in the group is 6
The total number of possible pairs is 76

Updated on: 01-Feb-2022

418 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements