Given a number **N** which represents the number of students, the task is to calculate all possible ways to rank them according to their CGPA/marks, considering that two or more students can have the same rank. Since the answer can be large, perform the modulo with 10^{9} + 7.

**Examples:**

Input:N = 1

Output:1

Explanation:

There is only one way to rank a student, irrespective of his marks.

Input:N = 2

Output:2

Explanation:

In the following two ways the ranks can be distributed among two students:

The high scoring student can be awarded the first rank and the low scoring student the second rank or both of them can be awarded the same rank if they have equal scores.

**Approach:** The idea for this problem is to use the Bell Numbers.

- A bell number is a number that counts the possible partitions of a set. Therefore, an N-th bell number is a number of non-empty subsets a set of size N can be partitioned into.
- For example, let’s consider the set {1, 2, 3} for N = 3. The bell number corresponding to N = 3 is 5. This signifies that the given set can be partitioned into following 5 non-empty subsets:
{{1}, {2}, {3}} {{1, 2}, {3}} {{1, 3}, {2}} {{2, 3}, {1}} {{1, 2, 3}}

- Clearly, the above bell numbers signify all the possible ranks. However, they do not calculate the permutations of the subset.
- Therefore, by multiplying every subset with K!, where K denotes the size of the respective subset, we get all the possible arrangements.
- As the same subproblems can be repeated, we can store the values of each subproblem in a data structure to optimize the complexity.

Below is the implementation of the above approach:

## C++

`// C++ program to calculate the number ` `// of ways to give ranks for N ` `// students such that same ranks ` `// are possible ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `int` `mod = 1e9 + 7; ` ` ` `// Initializing a table in order to ` `// store the bell triangle ` `vector<vector<` `int` `> > dp; ` ` ` `// Function to calculate the K-th ` `// bell number ` `int` `f(` `int` `n, ` `int` `k) ` `{ ` ` ` `// If we have already calculated ` ` ` `// the bell numbers until the ` ` ` `// required N ` ` ` `if` `(n < k) ` ` ` `return` `0; ` ` ` ` ` `// Base case ` ` ` `if` `(n == k) ` ` ` `return` `1; ` ` ` ` ` `// First Bell Number ` ` ` `if` `(k == 1) ` ` ` `return` `1; ` ` ` ` ` `// If the value of the bell ` ` ` `// triangle has already been ` ` ` `// calculated ` ` ` `if` `(dp[n][k] != -1) ` ` ` `return` `dp[n][k]; ` ` ` ` ` `// Fill the defined dp table ` ` ` `return` `dp[n][k] = ((k * f(n - 1, k)) % mod ` ` ` `+ (f(n - 1, k - 1)) % mod) ` ` ` `% mod; ` `} ` ` ` `// Function to return the number ` `// of ways to give ranks for N ` `// students such that same ranks ` `// are possible ` `long` `operation(` `int` `n) ` `{ ` ` ` `// Resizing the dp table for the ` ` ` `// given value of n ` ` ` `dp.resize(n + 1, vector<` `int` `>(n + 1, -1)); ` ` ` ` ` `// Variables to store the answer ` ` ` `// and the factorial value ` ` ` `long` `ans = 0, fac = 1; ` ` ` ` ` `// Iterating till N ` ` ` `for` `(` `int` `k = 1; k <= n; k++) { ` ` ` ` ` `// Simultaneously calculate the k! ` ` ` `fac *= k; ` ` ` ` ` `// Computing the K-th bell number ` ` ` `// and multiplying it with K! ` ` ` `ans = (ans + (fac * f(n, k)) % mod) ` ` ` `% mod; ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` ` ` `cout << operation(n) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to calculate the number ` `// of ways to give ranks for N ` `// students such that same ranks ` `// are possible ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `static` `int` `mod = (` `int` `)(1e9 + ` `7` `); ` ` ` `// Initializing a table in order to ` `// store the bell triangle ` `static` `int` `[][]dp; ` ` ` `// Function to calculate the K-th ` `// bell number ` `static` `int` `f(` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `// If we have already calculated ` ` ` `// the bell numbers until the ` ` ` `// required N ` ` ` `if` `(n < k) ` ` ` `return` `0` `; ` ` ` ` ` `// Base case ` ` ` `if` `(n == k) ` ` ` `return` `1` `; ` ` ` ` ` `// First Bell Number ` ` ` `if` `(k == ` `1` `) ` ` ` `return` `1` `; ` ` ` ` ` `// If the value of the bell ` ` ` `// triangle has already been ` ` ` `// calculated ` ` ` `if` `(dp[n][k] != -` `1` `) ` ` ` `return` `dp[n][k]; ` ` ` ` ` `// Fill the defined dp table ` ` ` `return` `dp[n][k] = ((k * f(n - ` `1` `, k)) % mod + ` ` ` `(f(n - ` `1` `, k - ` `1` `)) % mod) % mod; ` `} ` ` ` `// Function to return the number ` `// of ways to give ranks for N ` `// students such that same ranks ` `// are possible ` `static` `long` `operation(` `int` `n) ` `{ ` ` ` ` ` `// Resizing the dp table for the ` ` ` `// given value of n ` ` ` `dp = ` `new` `int` `[n + ` `1` `][n + ` `1` `]; ` ` ` `for` `(` `int` `i = ` `0` `; i < n + ` `1` `; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = ` `0` `; j < n + ` `1` `; j++) ` ` ` `{ ` ` ` `dp[i][j] = -` `1` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Variables to store the answer ` ` ` `// and the factorial value ` ` ` `long` `ans = ` `0` `, fac = ` `1` `; ` ` ` ` ` `// Iterating till N ` ` ` `for` `(` `int` `k = ` `1` `; k <= n; k++) ` ` ` `{ ` ` ` ` ` `// Simultaneously calculate the k! ` ` ` `fac *= k; ` ` ` ` ` `// Computing the K-th bell number ` ` ` `// and multiplying it with K! ` ` ` `ans = (ans + (fac * f(n, k)) % mod) % mod; ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` ` ` `System.out.print(operation(n) + ` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

## C#

`// C# program to calculate the number ` `// of ways to give ranks for N ` `// students such that same ranks ` `// are possible ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `static` `int` `mod = (` `int` `)(1e9 + 7); ` ` ` `// Initializing a table in order to ` `// store the bell triangle ` `static` `int` `[,]dp; ` ` ` `// Function to calculate the K-th ` `// bell number ` `static` `int` `f(` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `// If we have already calculated ` ` ` `// the bell numbers until the ` ` ` `// required N ` ` ` `if` `(n < k) ` ` ` `return` `0; ` ` ` ` ` `// Base case ` ` ` `if` `(n == k) ` ` ` `return` `1; ` ` ` ` ` `// First Bell Number ` ` ` `if` `(k == 1) ` ` ` `return` `1; ` ` ` ` ` `// If the value of the bell ` ` ` `// triangle has already been ` ` ` `// calculated ` ` ` `if` `(dp[n, k] != -1) ` ` ` `return` `dp[n, k]; ` ` ` ` ` `// Fill the defined dp table ` ` ` `return` `dp[n, k] = ((k * f(n - 1, k)) % mod + ` ` ` `(f(n - 1, k - 1)) % mod) % mod; ` `} ` ` ` `// Function to return the number ` `// of ways to give ranks for N ` `// students such that same ranks ` `// are possible ` `static` `long` `operation(` `int` `n) ` `{ ` ` ` ` ` `// Resizing the dp table for the ` ` ` `// given value of n ` ` ` `dp = ` `new` `int` `[n + 1, n + 1]; ` ` ` `for` `(` `int` `i = 0; i < n + 1; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = 0; j < n + 1; j++) ` ` ` `{ ` ` ` `dp[i, j] = -1; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Variables to store the answer ` ` ` `// and the factorial value ` ` ` `long` `ans = 0, fac = 1; ` ` ` ` ` `// Iterating till N ` ` ` `for` `(` `int` `k = 1; k <= n; k++) ` ` ` `{ ` ` ` ` ` `// Simultaneously calculate the k! ` ` ` `fac *= k; ` ` ` ` ` `// Computing the K-th bell number ` ` ` `// and multiplying it with K! ` ` ` `ans = (ans + (fac * f(n, k)) % mod) % mod; ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 5; ` ` ` ` ` `Console.Write(operation(n) + ` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

**Output:**

541

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of ways to distribute N Paper Set among M students
- Count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13
- Find smallest possible Number from a given large Number with same count of digits
- Count number of ways to divide an array into two halves with same sum
- Ways to paint N paintings such that adjacent paintings don't have same colors
- Print combinations of distinct numbers which add up to give sum N
- Minimum and maximum number of N chocolates after distribution among K students
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Count possible ways to construct buildings
- Count of all possible ways to reach a target by a Knight
- Maximum students to pass after giving bonus to everybody and not exceeding 100 marks
- Count of strings possible by replacing two consecutive same character with new character
- Count of all possible pairs of array elements with same parity
- Find the number of ways to divide number into four parts such that a = c and b = d
- Number of ways to split a binary number such that every part is divisible by 2
- Count ways to partition a string such that both parts have equal distinct characters
- Count number of ways to divide a number in 4 parts
- Count of all possible values of X such that A % X = B
- Count of distinct possible pairs such that the element from A is greater than the element from B
- Count of all possible Paths in a Tree such that Node X does not appear before Node Y

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.