Friends Pairing Problem

Given n friends, each one can remain single or can be paired up with some other friend. Each friend can be paired only once. Find out the total number of ways in which friends can remain single or can be paired up.

Examples :

Input  : n = 3
Output : 4

Explanation
{1}, {2}, {3} : all single
{1}, {2,3} : 2 and 3 paired but 1 is single.
{1,2}, {3} : 1 and 2 are paired but 3 is single.
{1,3}, {2} : 1 and 3 are paired but 2 is single.
Note that {1,2} and {2,1} are considered same.
f(n) = ways n people can remain single 
       or pair up.

For n-th person there are two choices:
1) n-th person remains single, we recur 
   for f(n - 1)
2) n-th person pairs up with any of the 
   remaining n - 1 persons. We get (n - 1) * f(n - 2)

Therefore we can recursively write f(n) as:
f(n) = f(n - 1) + (n - 1) * f(n - 2)

Since above recursive formula has overlapping subproblems, we can solve it using Dynamic Programming.

C++



filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program solution friends pairing problem
// C++ program for solution of
// friends pairing problem
#include <bits/stdc++.h>
using namespace std;
  
// Returns count of ways n people 
// can remain single or paired up.
int countFriendsPairings(int n)
{
    int dp[n + 1];
  
    // Filling dp[] in bottom-up manner using
    // recursive formula explained above.
    for (int i = 0; i <= n; i++)
    {
        if (i <= 2)
        dp[i] = i;
        else
        dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
    }
  
    return dp[n];
}
  
// Driver code
int main()
{
    int n = 4;
    cout << countFriendsPairings(n) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for solution of 
// friends pairing problem
import java.io.*;
  
class GFG 
{
      
    // Returns count of ways n people
    // can remain single or paired up.
    static int countFriendsPairings(int n)
    {
        int dp[] = new int[n + 1];
      
        // Filling dp[] in bottom-up manner using
        // recursive formula explained above.
        for (int i = 0; i <= n; i++)
        {
            if (i <= 2)
                dp[i] = i;
            else
                dp[i] = dp[i - 1] + (i - 1
                                * dp[i - 2];
        }
      
        return dp[n];
    }
      
    // Driver code
    public static void main (String[] args)
    {
        int n = 4;
        System.out.println (countFriendsPairings(n));
      
    }
}
  
// This code is contributed by vt_m

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program solution of
# friends pairing problem
  
# Returns count of ways
# n people can remain
# single or paired up.
def countFriendsPairings(n):
  
    dp = [0 for i in range(n + 1)]
  
    # Filling dp[] in bottom-up manner using
    # recursive formula explained above.
    for i in range(n + 1):
  
        if(i <= 2):
            dp[i] = i
        else:
            dp[i] = dp[i - 1] + (i - 1) * dp[i - 2]
  
    return dp[n]
  
# Driver code
n = 4
print(countFriendsPairings(n))
  
# This code is contributed
# by Soumen Ghosh.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program solution for 
// friends pairing problem
using System;
  
class GFG  
{
      
    // Returns count of ways n people
    // can remain single or paired up.
    static int countFriendsPairings(int n)
    {
        int []dp = new int[n + 1];
      
        // Filling dp[] in bottom-up manner using
        // recursive formula explained above.
        for (int i = 0; i <= n; i++)
        {
            if (i <= 2)
                dp[i] = i;
            else
                dp[i] = dp[i - 1] + (i - 1) 
                                * dp[i - 2];
        }
      
        return dp[n];
    }
      
    // Driver code
    public static void Main ()
    {
        int n = 4;
        Console.Write(countFriendsPairings(n));
      
    }
}
  
// This code is contributed by nitin mittal.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program solution for 
// friends pairing problem
  
// Returns count of ways n people 
// can remain single or paired up.
function countFriendsPairings($n)
{
    $dp[$n + 1] = 0;
  
    // Filling dp[] in bottom-up 
    // manner using recursive formula 
    // explained above.
    for ($i = 0; $i <= $n; $i++)
    {
        if ($i <= 2)
        $dp[$i] = $i;
        else
        $dp[$i] = $dp[$i - 1] + 
                     ($i - 1) * 
                   $dp[$i - 2];
    }
  
    return $dp[$n];
}
  
// Driver code
$n = 4;
echo countFriendsPairings($n) ;
      
// This code is contributed 
// by nitin mittal.
?>

chevron_right



Output :

10

Time Complexity : O(n)
Auxiliary Space : O(n)

Another approach: (Using recursion)

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for solution of friends
// pairing problem Using Recursion
#include<bits/stdc++.h>
using namespace std;
  
int dp[1000];
  
// Returns count of ways n people 
// can remain single or paired up.
int countFriendsPairings(int n)
{
    if(dp[n]!=-1)
    return dp[n];
  
    if(n>2) 
    return dp[n] = countFriendsPairings(n-1)+ 
                (n-1)*countFriendsPairings(n-2);
    else
    return dp[n] = n;
}
  
// Driver code
int main()
{
    memset(dp,-1,sizeof(dp));
int n = 4; 
    cout << countFriendsPairings(n) << endl; 
// this code is contributed by Kushdeep Mittal
}

chevron_right


Output :

10

Time Complexity : O(n)
Auxiliary Space : O(n)

This article is contributed by Ekta Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up