-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFriendsPairingProblem.cpp
More file actions
65 lines (65 loc) · 1.37 KB
/
FriendsPairingProblem.cpp
File metadata and controls
65 lines (65 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
using namespace std;
//Time complexity is O(n) And Space Complexity is O(n)
int fpp_bottom_up(int n)
{
int *dp=new int[n+1];
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];
}
//Time complexity is O(n) And Space Complexity is O(n)
int fpp_top_down(int *dp,int n)
{
if(dp[n]!=-1)
{
return dp[n];
}
if(n==0 || n==1 || n==2)
{
dp[n]=n;
return dp[n];
}
else
{
dp[n]=fpp_top_down(dp,n-1)+(n-1)*fpp_top_down(dp,n-2);
return dp[n];
}
}
//Time Complexity is O(n) and Space Complexity is O(1)
int fpp_fibonacci(int n)
{
int a=1;
int b=2;
int c=0;
for(int i=3;i<=n;i++)
{
c=b+a*(i-1);
a=b;
b=c;
}
return c;
}
int main()
{
int n;
cout<<"Enter the value of n:\n";
cin>>n;
cout<<"The number of friends pairing problem Using Bottom up is :"<<fpp_bottom_up(n)<<endl;
int *dp=new int[n+1];
for(int i=0;i<=n;i++)
{
dp[i]=-1;
}
cout<<"The number of friends pairing problem Using Top Down is :"<<fpp_top_down(dp,n)<<endl;
cout<<"The number of friends pairing problem Using Fibonacci is :"<<fpp_fibonacci(n)<<endl;
}