From be7ec2608cba2be8548e60a5eb7af60c039fd344 Mon Sep 17 00:00:00 2001 From: ashishkore <43812466+ashishkore@users.noreply.github.com> Date: Wed, 3 Oct 2018 16:51:54 +0530 Subject: [PATCH] permutationcoeff --- dynamic_programming/permutationcoeff.cpp | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 dynamic_programming/permutationcoeff.cpp diff --git a/dynamic_programming/permutationcoeff.cpp b/dynamic_programming/permutationcoeff.cpp new file mode 100644 index 0000000..e19814c --- /dev/null +++ b/dynamic_programming/permutationcoeff.cpp @@ -0,0 +1,32 @@ + +// A O(n) solution that uses +// table fact[] to calculate +// the Permutation Coefficient +#include + +// Returns value of Permutation +// Coefficient P(n, k) +int permutationCoeff(int n, int k) +{ + int fact[n + 1]; + + // base case + fact[0] = 1; + + // Caculate value + // factorials up to n + for (int i = 1; i <= n; i++) + fact[i] = i * fact[i - 1]; + + // P(n,k) = n! / (n - k)! + return fact[n] / fact[n - k]; +} + +// Driver Code +int main() +{ + int n = 10, k = 2; + printf ("Value of P(%d, %d) is %d ", + n, k, permutationCoeff(n, k) ); + return 0; +}