Following are the common definitions of Binomial Coefficients.
- A binomial coefficient C(n, k) can be defined as the coefficient of Xk in the expansion of (1 + X)n.
- A binomial coefficient C(n, k) also gives the number of ways, disregarding order, that k objects can be chosen from among n objects; more formally, the number of k-element subsets (or k-combinations) of an n-element set.
Given two numbers N and r, The task is to find the value of NCr
Examples :
Input: N = 5, r = 2
Output: 10
Explanation: The value of 5C2 is 10
Input: N = 3, r = 1
Output: 3
Approach: Below is the idea to solve the problem:
The total number of ways for selecting r elements out of n options are nCr = (n!) / (r! * (n-r)!)
where n! = 1 * 2 * . . . * n.
Below is the Implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int fact( int n);
int nCr( int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
int fact( int n)
{
if (n==0)
return 1;
int res = 1;
for ( int i = 2; i <= n; i++)
res = res * i;
return res;
}
int main()
{
int n = 5, r = 3;
cout << nCr(n, r);
return 0;
}
|
C
#include <stdio.h>
int factorial( int n) {
if (n == 0)
return 1;
int factorial = 1;
for ( int i = 2; i <= n; i++)
factorial = factorial * i;
return factorial;
}
int nCr( int n, int r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
int main() {
int n = 5, r = 3;
printf ( "%d" , nCr(n, r));
return 0;
}
|
Java
import java.io.*;
public class GFG {
static int nCr( int n, int r)
{
return fact(n) / (fact(r) *
fact(n - r));
}
static int fact( int n)
{
if (n== 0 )
return 1 ;
int res = 1 ;
for ( int i = 2 ; i <= n; i++)
res = res * i;
return res;
}
public static void main(String[] args)
{
int n = 5 , r = 3 ;
System.out.println(nCr(n, r));
}
}
|
Python 3
def nCr(n, r):
return (fact(n) / (fact(r)
* fact(n - r)))
def fact(n):
if n = = 0 :
return 1
res = 1
for i in range ( 2 , n + 1 ):
res = res * i
return res
n = 5
r = 3
print ( int (nCr(n, r)))
|
C#
using System;
class GFG {
static int nCr( int n, int r)
{
return fact(n) / (fact(r) *
fact(n - r));
}
static int fact( int n)
{
if (n==0)
return 1;
int res = 1;
for ( int i = 2; i <= n; i++)
res = res * i;
return res;
}
public static void Main()
{
int n = 5, r = 3;
Console.Write(nCr(n, r));
}
}
|
Javascript
<script>
function nCr(n, r)
{
return fact(n) / (fact(r) * fact(n - r));
}
function fact(n)
{
if (n==0)
return 1;
var res = 1;
for ( var i = 2; i <= n; i++)
res = res * i;
return res;
}
var n = 5, r = 3;
document.write(nCr(n, r));
</script>
|
PHP
<?php
function nCr( $n , $r )
{
return fact( $n ) / (fact( $r ) *
fact( $n - $r ));
}
function fact( $n )
{
if ( $n == 0)
return 1;
$res = 1;
for ( $i = 2; $i <= $n ; $i ++)
$res = $res * $i ;
return $res ;
}
$n = 5;
$r = 3;
echo nCr( $n , $r );
?>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Complexity Analysis:
The time complexity of the above approach is O(N).
This is because the function fact() has a time complexity of O(N), and it is called twice for each call to nCr().
The space complexity of the above approach is O(1).
Because the function does not make any recursive calls and only uses a constant amount of memory.
Another Approach:
The idea is to use a recursive function to calculate the value of nCr. The base cases are:
- if r is greater than n, return 0 (there are no combinations possible)
- if r is 0 or r is n, return 1 (there is only 1 combination possible in these cases)
For other values of n and r, the function calculates the value of nCr by adding the number of combinations possible by including the current element and the number of combinations possible by not including the current element.
Below is the Implementation of the above approach:
C++
#include <iostream>
using namespace std;
int nCr( int n, int r)
{
if (r > n)
return 0;
if (r == 0 || r == n)
return 1;
return nCr(n - 1, r - 1) + nCr(n - 1, r);
}
int main()
{
cout << nCr(5, 3);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int nCr( int n, int r)
{
if (r > n)
return 0 ;
if (r == 0 || r == n)
return 1 ;
return nCr(n - 1 , r - 1 ) + nCr(n - 1 , r);
}
public static void main(String[] args)
{
System.out.println(nCr( 5 , 3 ));
}
}
|
Python3
def nCr(n, r):
if r > n:
return 0
if r = = 0 or r = = n:
return 1
return nCr(n - 1 , r - 1 ) + nCr(n - 1 , r)
print (nCr( 5 , 3 ))
|
C#
using System;
public class GFG {
static public int nCr( int n, int r)
{
if (r > n)
return 0;
if (r == 0 || r == n)
return 1;
return nCr(n - 1, r - 1) + nCr(n - 1, r);
}
static public void Main( string [] args)
{
Console.WriteLine(nCr(5, 3));
}
}
|
Javascript
function nCr(n, r) {
if (r > n)
return 0;
if (r === 0 || r === n)
return 1;
return nCr(n-1, r-1) + nCr(n-1, r);
}
console.log(nCr(5, 3));
|
Time Complexity: O(2N)
Auxiliary Space: O(N2)
More efficient approach:
Iterative way of calculating NCR. using binomial coefficient formula.
C++
#include <iostream>
using namespace std;
int main() {
int n = 5;
int r = 2;
double sum = 1;
for ( int i = 1; i <= r; i++){
sum = sum * (n - r + i) / i;
}
cout<<( int )sum<<endl;
return 0;
}
|
Java
import java.util.*;
public class BinomialCoefficient {
public static void main(String[] args)
{
int n = 5 ;
int r = 2 ;
double sum = 1 ;
for ( int i = 1 ; i <= r; i++) {
sum = sum * (n - r + i) / i;
}
System.out.println(( int )sum);
}
}
|
Python3
n = 5
r = 2
sum = 1
for i in range ( 1 , r + 1 ):
sum = sum * (n - r + i) / i
print ( int ( sum ))
|
C#
using System;
class HelloWorld {
static void Main() {
int n = 5;
int r = 2;
double sum = 1;
for ( int i = 1; i <= r; i++){
sum = sum * (n - r + i) / i;
}
Console.WriteLine(( int )sum);
}
}
|
Javascript
let n = 5;
let r = 2;
let sum = 1;
for (let i = 1; i <= r; i++){
sum = sum * (n - r + i) / i;
}
console.log(Math.floor(sum));
|
Time complexity : O(r)
Space complexity : O(1)
Another Approach(Using Logarithmic Formula):
Logarithmic formula for nCr is an alternative to the factorial formula that avoids computing factorials directly and it’s more efficient for large values of n and r. It uses the identity log(n!) = log(1) + log(2) + … + log(n) to express the numerator and denominator of the nCr in terms of sums of logarithms which allows to calculate the nCr using the Logarithmic operations. This approach is faster and very efficient.
The logarithmic formula for nCr is:
nCr = exp( log(n!) – log(r!) – log((n-r)!) )
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int nCr( int n, int r) {
if (r > n) return 0;
if (r == 0 || n == r) return 1;
double res = 0;
for ( int i = 0; i < r; i++) {
res += log (n-i) - log (i+1);
}
return ( int )round( exp (res));
}
int main() {
int n = 5;
int r = 2;
cout << nCr(n, r) << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int nCr( int n, int r) {
if (r > n)
return 0 ;
if (r == 0 || n == r)
return 1 ;
double res = 0 ;
for ( int i = 0 ; i < r; i++) {
res += Math.log(n - i) - Math.log(i + 1 );
}
return ( int ) Math.round(Math.exp(res));
}
public static void main(String[] args) {
int n = 5 ;
int r = 2 ;
System.out.println(nCr(n, r));
}
}
|
Python
import math
def nCr(n, r):
if r > n:
return 0
if r = = 0 or n = = r:
return 1
res = 0
for i in range (r):
res + = math.log(n - i) - math.log(i + 1 )
return round (math.exp(res))
n = 5
r = 2
print (nCr(n, r))
|
C#
using System;
namespace BinomialCoefficient
{
class Program
{
static int nCr( int n, int r)
{
if (r > n) return 0;
if (r == 0 || n == r) return 1;
double res = 0;
for ( int i = 0; i < r; i++)
{
res += Math.Log(n - i) - Math.Log(i + 1);
}
return ( int )Math.Round(Math.Exp(res));
}
static void Main( string [] args)
{
int n = 5;
int r = 2;
Console.WriteLine(nCr(n, r));
}
}
}
|
Javascript
function nCr(n, r) {
if (r > n) return 0;
if (r === 0 || n === r) return 1;
let res = 0;
for (let i = 0; i < r; i++) {
res += Math.log(n - i) - Math.log(i + 1);
}
return Math.round(Math.exp(res));
}
const n = 5;
const r = 2;
console.log(nCr(n, r));
|
Time Complexity: O(r)
Auxiliary Space: O(1)
More Efficient Solutions:
Dynamic Programming | Set 9 (Binomial Coefficient)
Space and time efficient Binomial Coefficient
All Articles on Binomial Coefficient
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
18 Sep, 2023
Like Article
Save Article