-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBellNumber.cpp
More file actions
54 lines (52 loc) · 1.09 KB
/
BellNumber.cpp
File metadata and controls
54 lines (52 loc) · 1.09 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
#include<iostream>
using namespace std;
//Time Complexity is O(n^2) And Space Complexity is O(n^2)
int bellDP(int num)
{
int **ar=new int*[num+1];
for(int i=0;i<num+1;i++)
{
ar[i]=new int[num+1];
}
for(int i=0;i<=num;i++)
{
for(int j=0;j<=num;j++)
{
ar[i][j]=0;
}
}
ar[0][0]=1;
for(int i=1;i<=num;i++)
{
ar[i][0]=ar[i-1][i-1];
for(int j=1;j<=i;j++)
{
ar[i][j]=ar[i][j-1]+ar[i-1][j-1];
}
}
return ar[num-1][num-1];
}
//Time complexity is exponential And Space Complexity is O(n) in Stack
int bellRec(int i,int j)
{
if(i==0 && j==0)
{
return 1;
}
else if(j==0)
{
return bellRec(i-1,i-1);
}
else
{
return bellRec(i-1,j-1)+bellRec(i,j-1);
}
}
int main()
{
int n;
cout<<"Enter the value of n:"<<endl;
cin>>n;
cout<<"The bell number at Using DP "<<n<<" is: "<<bellDP(n)<<endl;
cout<<"The bell number at Using Recursion "<<n<<" is: "<<bellRec(n-1,n-1);
}