Write a function that takes two parameters n and k and returns the value of Binomial Coefficient C(n, k). Example:
Input: n = 4 and k = 2
Output: 6
Explanation: 4 C 2 is 4!/(2!*2!) = 6
Input: n = 5 and k = 2
Output: 10
Explanation: 5 C 2 is 5!/(3!*2!) = 20We have discussed a O(n*k) time and O(k) extra space algorithm in this post. The value of C(n, k) can be calculated in O(k) time and O(1) extra space.
Solution:
C(n, k)
= n! / (n-k)! * k!
= [n * (n-1) *....* 1] / [ ( (n-k) * (n-k-1) * .... * 1) *
( k * (k-1) * .... * 1 ) ]
After simplifying, we get
C(n, k)
= [n * (n-1) * .... * (n-k+1)] / [k * (k-1) * .... * 1]
Also, C(n, k) = C(n, n-k)
// r can be changed to n-r if r > n-r Change r to n-r if r is greater than n-r. and create a variable to store the answer. Run a loop from 0 to r-1 In every iteration update ans as (ans*(n-i))/(i+1) where i is the loop counter. So the answer will be equal to ((n/1)*((n-1)/2)*…*((n-r+1)/r!) which is equal to nCr. Following implementation uses above formula to calculate C(n, k).
C++
#include <bits/stdc++.h>
using
namespace
std;
int
binomialCoeff(
int
n,
int
k)
{
int
res = 1;
if
(k > n - k)
k = n - k;
for
(
int
i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return
res;
}
int
main()
{
int
n = 8, k = 2;
cout <<
"Value of C("
<< n <<
", "
<< k <<
") is "
<< binomialCoeff(n, k);
return
0;
}
C
#include <stdio.h>
int
binomialCoeff(
int
n,
int
k)
{
int
res = 1;
if
(k > n - k)
k = n - k;
for
(
int
i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return
res;
}
int
main()
{
int
n = 8, k = 2;
printf
(
"Value of C(%d, %d) is %d "
,
n, k, binomialCoeff(n, k));
return
0;
}
Java
class
BinomialCoefficient {
static
int
binomialCoeff(
int
n,
int
k)
{
int
res =
1
;
if
(k > n - k)
k = n - k;
for
(
int
i =
0
; i < k; ++i) {
res *= (n - i);
res /= (i +
1
);
}
return
res;
}
public
static
void
main(String[] args)
{
int
n =
8
;
int
k =
2
;
System.out.println(
"Value of C("
+ n +
", "
+ k +
") "
+
"is"
+
" "
+ binomialCoeff(n, k));
}
}
Python3
def
binomialCoefficient(n, k):
if
(k > n
-
k):
k
=
n
-
k
res
=
1
for
i
in
range
(k):
res
=
res
*
(n
-
i)
res
=
res
/
/
(i
+
1
)
return
res
n
=
8
k
=
2
res
=
binomialCoefficient(n, k)
print
(
"Value of C(% d, % d) is % d"
%
(n, k, res))
C#
using
System;
class
BinomialCoefficient {
static
int
binomialCoeff(
int
n,
int
k)
{
int
res = 1;
if
(k > n - k)
k = n - k;
for
(
int
i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return
res;
}
public
static
void
Main()
{
int
n = 8;
int
k = 2;
Console.Write(
"Value of C("
+ n +
", "
+ k +
") "
+
"is"
+
" "
+ binomialCoeff(n, k));
}
}
PHP <?php
function
binomialCoeff(
$n
,
$k
)
{
$res
= 1;
if
(
$k
>
$n
-
$k
)
$k
=
$n
-
$k
;
for
(
$i
= 0;
$i
<
$k
; ++
$i
)
{
$res
*= (
$n
-
$i
);
$res
/= (
$i
+ 1);
}
return
$res
;
}
$n
= 8;
$k
= 2;
echo
" Value of C ($n, $k) is "
,
binomialCoeff(
$n
,
$k
);
?>
Javascript <script>
function
binomialCoeff(n, k)
{
let res = 1;
if
(k > n - k)
k = n - k;
for
(let i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return
res;
}
let n = 8;
let k = 2;
document.write(
"Value of C("
+ n +
", "
+ k +
") "
+
"is"
+
" "
+ binomialCoeff(n, k));
</script>
Output:
Value of C(8, 2) is 28 Complexity Analysis:
Time Complexity: O(r). A loop has to be run from 0 to r. So, the time complexity is O(r).Auxiliary Space: O(1). As no extra space is required.VIDEO
This article is compiled by Aashish Barnwal and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Vote for difficulty
Easy
Normal
Medium
Hard
Expert