From 6273440e597512df0cc00d7ad8ea03333db3d886 Mon Sep 17 00:00:00 2001 From: ranganeeraj <103856631+ranganeeraj@users.noreply.github.com> Date: Sun, 1 Oct 2023 23:09:16 +0530 Subject: [PATCH] DP Solution for Binomial Coefficient --- .../Binomial Coefficient/SolutionByNeeraj.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Dynamic Programming/Binomial Coefficient/SolutionByNeeraj.cpp diff --git a/Dynamic Programming/Binomial Coefficient/SolutionByNeeraj.cpp b/Dynamic Programming/Binomial Coefficient/SolutionByNeeraj.cpp new file mode 100644 index 000000000..202dcf488 --- /dev/null +++ b/Dynamic Programming/Binomial Coefficient/SolutionByNeeraj.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; + +int binomialCoeff(int n, int k) { + int C[k+1]; + memset(C, 0, sizeof(C)); + + C[0] = 1; + + for (int i = 1; i <= n; i++) { + for (int j = min(i, k); j > 0; j--) + C[j] = C[j] + C[j-1]; + } + return C[k]; +} + +int main () { + cout << "\nEnter n\t:\t"; + int number; + cin >> number; + cout << "\nEnter k\t:\t"; + int k; + cin >> k; + cout <<"\nThe result is\t:\t" << binomialCoeff(number, k); + cout << endl; + return 0; +}