Bell Numbers | Practice | GeeksforGeeks

Given a set of n elements, find number of ways of partitioning it.
 
Example 1:
Input:
N = 2
Output: 2
Explanation:
Let the set be 
{1, 2}:
{ {1}, {2} } { {1, 2} }
 
Example 2:
Input:
N = 3
Output: 5


Your Task:  
You don't need to read input or print anything. Your task is to complete the function bellNumber() which takes the integer N as input parameters and returns the number of ways of partitioning n elements. Since the value can be quite large print the value modulo 109+7.

Expected Time Complexity: O(N^2)
Expected Auxiliary Space: O(N^2)
 
Constraints:
1 ≤ N ≤ 1000
0
Jyoti9 months ago
Jyoti
int bellNumber(int n)    {       // Code Here       int dp[n][n];       dp[0][0]=1;       int k=1e9+7;       for(int i=1;i<n;i++)<br>       {           dp[i][0]=dp[i-1][i-1];           for(int j=1;j<=i;j++)           {               dp[i][j]=(dp[i][j-1]+dp[i-1][j-1])%k;           }       }       return dp[n-1][n-1];    }
0
Rohith Vazhathody9 months ago
Rohith Vazhathody
class Solution{        int bellNumber(int n){        // Code Here        int mod = (int) (1e9 + 7);        long [][] dp = new long [n + 1][n + 1];        dp[0][0] = 1;        for (int i=1; i<=n; i++) {            dp[i][0] = dp[i - 1][i - 1]; // previous row, previous col            for (int j=1; j<=i; j++) {                dp[i][j] = (dp[i][j - 1] + dp[i - 1][j - 1]) % mod;            }        }        return (int)(dp[n - 1][n - 1] % mod);    }    }/*11  22  3. 55  7. 10 1515 20 27 37 52.........This is bell traingle*/
0
Chandru9 months ago
Chandru
C++ Correct Answer:✅✅✅
int bellNumber(int n)    {       int mod = 1e9 + 7;       int dp[n+1][n+1];       dp[0][0]=1;       for(int i=1;i<=n;i++)       {           dp[i][0] = dp[i-1][i-1];           for(int j=1;j<=i;j++)           {               dp[i][j] = (dp[i-1][j-1]+dp[i][j-1])%mod;           }       }       return dp[n-1][n-1];    }
0
ashutoshvimal229 months ago
ashutoshvimal22
If you are not able to understand the output then watch this video  https://www.youtube.com/wat...
0
i_am_chitti9 months ago
i_am_chitti
int bellNumber(int n)    {       // Code Here       long long dp[n+1][n+1];       dp[1][1]=1;              long long MOD = 1000000007;              for(int i=2;i<=n;i++) {                      dp[i][1]=dp[i-1][i-1];                      for(int j=2;j<=i;j++) {                              dp[i][j] = ((dp[i][j-1]%MOD)+(dp[i-1][j-1]%MOD))%MOD;                          }                  }              return dp[n][n];           }
0
Saranya Maity9 months ago
Saranya Maity
how its an easy question!
Hints save the day ! Go and understand Bell triangle given in hints !rest apply dp for the rest :/
One thing that's unacceptable is that how a single variable problem turn into a 2D DP problem which is solvable by Bell Triangle and how the DP for the bell triangle assuring partitioning OR how the intuition came ? Nothing is clear ,
0
Sakshi Agarwal9 months ago
Sakshi Agarwal
class Solution{    public:    int padovanSequence(int n)    {       //code here       int dp[n+1];       dp[0]=dp[1]=dp[2]=1;       for(int i=3;i<=n;i++){           dp[i] = (dp[i-2]+dp[i-3])%1000000007;       }        return dp[n];    } };
0
AnimeCringeLord11 months ago
AnimeCringeLord
Python3 Sol using Bell Triangle
https://uploads.disquscdn.c...
0
Ankit Rai2 years ago
Ankit Rai
#include <bits stdc++.h="">using namespace std;
int kset(int n){        long int mod=1000000007;        long int sum=0;    int a[n+1][n+1];    for(int i=1;i<=n;i++){        for(int j=1;j<=i;j++){            if(j==1 || i==j)            a[i][j]=1;            else            a[i][j]=((j*a[i-1][j])%mod + (a[i-1][j-1])%mod)%mod;        }    }    for(int j=1;j<=n;j++){        sum+=a[n][j];        //sum=sum%mod;    }    return sum;}int main() {int t;cin>>t;while(t--){    int n;    cin>>n;    cout<<kset(n)<<endl; }="" return="" 0;="" }="" what="" is="" wrong="" in="" this="" code?(wrong="" answer="" at="" 916)="">
0
RAJIV RAM S2 years ago
RAJIV RAM S
#include<bits stdc++.h="">using namespace std;int bellDP(int num){    int arr[num+1][num+1];    arr[0][0]=1;    arr[0][1]=2;    for(int i=1;i<=num;i++)    {        arr[i][0]=(arr[i-1][i-1])%(1000000007);        for(int j=1;j<=i;j++)        {            arr[i][j]=(arr[i-1][j-1]+arr[i][j-1])%(1000000007);;        }    }    return arr[num][0];}
int main() {     int t;     cin>>t;     while(t--)     {         int n;         cin>>n;       int res =  bellDP(n);           cout<<res<<endl; }="" return="" 0;="" }="">
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.