Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions DSA/Java/Dynamic_Programming/nCr Calculation/Problem Statement
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Find nCr for given n and r.

Example 1:

Input:
n = 3, r = 2
Output: 3
Example 2:

Input:
n = 4, r = 2
Output: 6
Your Task:
Complete the function nCrModp() which takes two integers n and r as input parameters and returns the nCr mod 109+7.
Note: nCr does not exist when r > n. Hence, return 0 in that case.

Expected Time Complexity : O(N*R)
Expected Auxiliary Space: O(N)

Constraints:
1<= n <= 103
1<= r <= 103

Company Tags
Amazon
35 changes: 35 additions & 0 deletions DSA/Java/Dynamic_Programming/nCr Calculation/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution
{

static int p = 1000000007;

public static int min(int a, int b)
{
return (a<b)? a: b;
}

//Function to return nCr mod 10^9+7 for given n and r.
public static int nCrModp(int n, int r)
{
//array C[] is going to store last row of pascal triangle at the end.
//last entry of last row is nCr.
int C[] = new int[r+1];
Arrays.fill(C, 0);

//top row of Pascal Triangle
C[0] = 1;

//one by one constructing remaining rows of Pascal
//triangle from top to bottom.
for (int i = 1; i <= n; i++)
{
//filling entries of current row using previous row values.
for (int j = min(i, r); j > 0; j--)
// nCj = (n-1)Cj + (n-1)C(j-1);
C[j] = (C[j]%p + C[j-1]%p)%p;
}

//returning the result.
return C[r];
}
}