Friends Pairing Problem | Practice | GeeksforGeeks

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.
Note: Since answer can be very large, return your answer mod 10^9+7.

Example 1:
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.

Example 2: 
Input: N = 2
Output: 2
Explanation:
{1} , {2} : All single.
{1,2} : 1 and 2 are paired.


Your Task:
You don't need to read input or print anything. Your task is to complete the function countFriendsPairings() which accepts an integer n and return number of ways in which friends can remain single or can be paired up.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N ≤ 104
+1
gardnerchris1 day ago
		int M=1e9+7;
        if(n==1||n==2) return n;
        long long prev=1, curr=2;
        for(int i=3; i<=n; i++) {
            long long temp=(curr%M+((i-1)*prev)%M)%M;
            prev=curr;
            curr=temp;
        }
        return curr;
+1
arthurshelby4 days ago
😉✌️🇮🇳❄️
long long int f(int n,vector<int>&dp)
    {
        if(n==3)
        return 4;
        if(n==2)
        return 2;
        if(n==1)
        return 1;
        if(dp[n]!=-1)
        return dp[n];
        return dp[n]=(f(n-1,dp)+(n-1)*f(n-2,dp))%1000000007;
    }
    int countFriendsPairings(int n) 
    { 
        // code here
        vector<int>dp(n+1,-1);
        return f(n,dp);
    }
0
noeldcostaec193 weeks ago
O(1) Auxilary Space
O(N) Time Complexity
 
class Solution:   def countFriendsPairings(self, n):       if n<=2:           return n       p1=2       p2=1       temp=0       for i in range(n-2,0,-1):           temp=p1           p1=p2*(n-i)+p1           p2=temp       return p1%(1000000007)
+1
kronizerdeltac3 weeks ago
JAVA TABULATION CODE:
 
int mod = (int) 1e9 + 7;
    public long countFriendsPairings_tabu(int N, long[] dp) {
        for(int n = 0; n <= N; n++) {
            if(n < 0)
                return 0;
    
            if(n == 0) {
                dp[n] = 1;
                continue;
            }
            
            int count = 0;
            count += dp[n - 1] % mod;
            if(n - 2 >= 0)
                count += dp[n - 2] * (n - 1) % mod;
            dp[n] = count % mod;
        }
        return dp[N];
    }
    
    public long countFriendsPairings(int n) {
        long[] dp = new long[n + 1];
        Arrays.fill(dp, -1);

        return countFriendsPairings_tabu(n, dp) % mod;
    }
0
milindprajapatmst193 weeks ago
# define ll long long
const int MOD = 1e9 + 7;
ll _add(ll x, ll y) {
    return (x + y) % MOD;
}
ll _mul(ll x, ll y) {
    return (x * y) % MOD;
}
class Solution {
public:
    int countFriendsPairings(int n) { 
        ll dp[2] = {1, 1};
        for (int x = 1; x <= n; x++) {
            ll T = dp[0];
            dp[0] = dp[1];
            dp[1] = _add(dp[1], _mul(x, T));
        }
        return dp[0];
    }
};   
+3
arorapankaj4711 month ago
O(N) time,  O(1) space Easiest Understandable solution.
int countFriendsPairings(int n){ 
        int mod=1e9+7;
        long long prevResult2 = 1;
        long long prevResult1 = 2;
        if(n == 0) return 0;
        else if(n == 1) return 1;
        else if(n == 2) return 2;
        for(int i = 3; i<=n; i++){
            long long prevResult = prevResult1 + (i-1)*prevResult2;
            prevResult %= mod;
            prevResult2 = prevResult1;
            prevResult1 = prevResult;
        }
        return prevResult1;
    }
-2
arunahircse231 month ago
All testcases passed : 
 int countFriendsPairings(int n)    {        // code here       long long int dp[10001];       dp[0]=0;       dp[1]=1;       dp[2]=2;       int mod=1e9+7;       for(int i=3; i<n+1; i++)       {           dp[i]=((dp[i-1]%mod) + ( ((dp[i-2])%mod) * ((i-1)%mod) )%mod)%mod;       }              return dp[n];   }
0
jainmuskan5651 month ago
 long long int dp[10001];   long long int mod= 1e9+7;   int helper(int n){       if(n<=2){           return n;       }       if(dp[n]!= -1){           return dp[n];       }       long long int a= helper(n-1)%mod;       long long int b= ((n-1)%mod)*(helper(n-2)%mod)%mod;       return dp[n]=(a+b)%mod;   }   int countFriendsPairings(int n)    {        // when nth person is single        // recur for f(n-1)       // if paired then n-1 * f(n-2)       memset(dp,-1,sizeof(dp));       return helper(n)%mod;   }
+19
bharatgupta688231 month ago
Tagging this problem easy is a criminal offence 
 
+3
aloksinghbais022 months ago
C++ solution using memoization (top down dp) having time complexity as O(N) and space complexity as O(N) is as follows :- 
 
Execution Time :- 0.0 / 1.1 sec
 
int mod = (int)1e9 + 7;   long long int dp[10001];      long long int helper(int n){       if(n == 1) return (1);       if(n == 2) return (2);              if(dp[n] != -1) return (dp[n]);              return dp[n] = (helper(n-1) + (n-1)*helper(n-2)) % mod;   }      int countFriendsPairings(int n){       memset(dp,-1,sizeof(dp));       return (int)helper(n);   }
 
C++ solution using bottom up dp having time complexity as O(N) and space complexity as O(N) is as follows :- 
 
Execution Time :- 0.0 / 1.1 sec
 
int countFriendsPairings(int n){       int mod = (int)1e9 + 7;       long long int dp[n+1];       dp[1] = 1;       dp[2] = 2;              for(int i = 3; i <= n; i++){           dp[i] = (dp[i-1] + (i-1)*dp[i-2]) % mod;       }              return (int)dp[n];   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.