File tree 1 file changed +52
-0
lines changed
1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Implementing Friend-Pairing problem using dynamic programming
2
+ // hacktoberfest2021
3
+
4
+ // https://practice.geeksforgeeks.org/problems/friends-pairing-problem5425/1#
5
+ #include < bits/stdc++.h>
6
+ using namespace std ;
7
+
8
+ // } Driver Code Ends
9
+ class Solution
10
+ {
11
+ public:
12
+ // Function to return the total number of ways in which 'n' friends can remain single or can be paired up.
13
+ int countFriendsPairings (int n)
14
+ {
15
+ // Your code here
16
+ long long int mod=1000000000 +7 ;
17
+ long long int dp[n+1 ];
18
+ for (int i=0 ;i<=n;i++)
19
+ {
20
+ if (i<=2 )
21
+ {
22
+ dp[i]=i%mod;
23
+ }
24
+ else
25
+ {
26
+ dp[i]=(dp[i-1 ]%mod+(i-1 )*(dp[i-2 ]%mod))%mod;
27
+ }
28
+ }
29
+
30
+ return dp[n]%(mod);
31
+ }
32
+ };
33
+
34
+
35
+ // { Driver Code Starts.
36
+ int main ()
37
+ {
38
+ // taking total testcases
39
+ int t;
40
+ cin>>t;
41
+ while (t--)
42
+ {
43
+ // reading total friends
44
+ int n;
45
+ cin>>n;
46
+ Solution ob;
47
+ // calling method countFriendsPairings
48
+ cout <<ob.countFriendsPairings (n);
49
+ cout<<endl;
50
+ }
51
+ }
52
+ // } Driver Code Ends
You can’t perform that action at this time.
0 commit comments