Approach 5 : Using Efficient Computation of Combinations

The formula for nCr is = n! / r! * (n-r) ! . Let’s imagine r > n-r (or can take reverse ) . Logic here is since in n! and r! we are going to calculate the factorial of r numbers twice and which is no use i.e they will just cancel out each other , so we don’t calculate them instead we would calculate the product of number from (r , n] and product of numbers from 1 to n-r and divide them (here r can be imagined as the maximum of r , n-r ) . Below is the code for above implementation

C++
#include <iostream>

// calculate multiplication of natural numbers from start to end 
double Multiplier(int start, int end)
{
    if (start == end) {
        return start;
    }
    else {
        double res = 1;
        while (start <= end) {
            res *= start;
            start++;
        }
        return res;
    }
}

double nCR(int n, int r)
{
    if (n < r) {
        return -1;
    }
    else if (n == r || r == 0) {
        return 1;
    }
    else {
        int max_val = std::max(r, n - r);
        int min_val = std::min(r, n - r);
        double numerator = Multiplier(max_val + 1, n);
        double denominator = Multiplier(1, min_val);
        return numerator / denominator;
    }
}

int main()
{
    int n = 20005;
    int r = 25;
    std::cout << nCR(n, r) << std::endl;
    return 0; 
  // contributed by Naveen (naveenkumar30838)
}
Java
/*package whatever //do not write package name here */

import java.io.*;

class GFG {
    public static double nCR(int n, int r)
    {
        if (n < r) {
            return -1;
        }
        if (n == r || r == 0) {
            return 1;
        }
        int max = Math.max(r, n - r);
        int min = Math.min(r, n - r);
        return (double)(Multiplier(max + 1, n)
                        / Multiplier(1, min));
    }
    // calculate multiplication of natural numbers from start to end 
    public static double Multiplier(int start, int end)
    {
        if (start == end) {
            return start;
        }
        double res = 1;
        while (start <= end) {
            res *= start;
            start++;
        }
        return res;
    }
    public static void main(String[] args)
    {
        System.out.println(
            nCR(20005, 25)); // ans 2.1443850196899737E82
        // Code contributed by Naveen (naveenkumar30838)
    }
}
Python
def nCR(n, r):
    if n < r:
        return -1
    if n == r or r == 0:
        return 1
    max_val = max(r, n - r)
    min_val = min(r, n - r)
    return Multiplier(max_val + 1, n) / Multiplier(1, min_val)

   # calculate multiplication of natural numbers from start to end 
def Multiplier(start, end):
    if start == end:
        return start
    res = 1
    while start <= end:
        res *= start
        start += 1
    return res


print(nCR(20005, 25))
#contributed by Naveen (naveenkumar30838) 
# ans 21443850196899739431852611539526726818562627337667639676891232267646561961531437300
C#
using System;

class GFG {
    static double nCR(int n, int r)
    {
        if (n < r) {
            return -1;
        }
        else if (n == r || r == 0) {
            return 1;
        }
        else {
            int max_val = Math.Max(r, n - r);
            int min_val = Math.Min(r, n - r);
            double numerator = Multiplier(max_val + 1, n);
            double denominator = Multiplier(1, min_val);
            return numerator / denominator;
        }
    }
    // calculate multiplication of natural numbers from start to end 
    static double Multiplier(int start, int end)
    {
        if (start == end) {
            return start;
        }
        else {
            double res = 1;
            while (start <= end) {
                res *= start;
                start++;
            }
            return res;
        }
    }

    static void Main(string[] args)
    {
        int n = 20005;
        int r = 25;
        Console.WriteLine($ "{nCR(n, r)}");
      // Contributed by Naveen(naveenkumar30838) 
     // ans 2.1443850196899737E82
      
    }
}
JavaScript
// Calculate multiplication of natural numbers from start to end
function multiplier(start, end) {
    if (start === end) {
        return start;
    } else {
        let res = 1;
        while (start <= end) {
            res *= start;
            start++;
        }
        return res;
    }
}

// Function to calculate n choose r (nCr)
function nCR(n, r) {
    if (n < r) {
        return -1;
    } else if (n === r || r === 0) {
        return 1;
    } else {
        const maxVal = Math.max(r, n - r);
        const minVal = Math.min(r, n - r);
        const numerator = multiplier(maxVal + 1, n);
        const denominator = multiplier(1, minVal);
        return numerator / denominator;
    }
}

// Example usage
const n = 20005;
const r = 25;
console.log(nCR(n, r));

Output : 2.1443850196899737E82

Time complexity : O(n)

Space complexity : O(1)

Complexity Analysis : In every case (including worst case ) we have to calculate the multiplier from (n-r) and from (1 to r ) which give run in linear time giving a total time complexity of O(n-r +r ) = O(n) . Since no extra space is used so space complexity if O(1) . Since we have eliminated the repeated steps so it is a more optimized solution and hence it can be used for handling large values .

Program to calculate value of nCr

Given two numbers N and r, The task is to find the value of NCr . Combinations represent the number of ways to choose r elements from a set of n distinct elements, without regard to the order in which they are selected. The formula for calculating combinations is :

C(n,r) = n! / r! * (n-r) !

Where :

  • (n!) represents the factorial of n .
  • (r!) represents the factorial of r .

Examples : 

Input: N = 5, r = 2
Output: 10 
Explanation: The value of 5C2 is 10

Input: N = 3, r = 1
Output: 3

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Similar Reads

Program to Calculate the value of nCr

Approach 1 : Using Factorial...

Approach 4 : 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....

Approach 5 : Using Efficient Computation of Combinations

The formula for nCr is = n! / r! * (n-r) ! . Let’s imagine r > n-r (or can take reverse ) . Logic here is since in n! and r! we are going to calculate the factorial of r numbers twice and which is no use i.e they will just cancel out each other , so we don’t calculate them instead we would calculate the product of number from (r , n] and product of numbers from 1 to n-r and divide them (here r can be imagined as the maximum of r , n-r ) . Below is the code for above implementation...

Conclusion

In conclusion, nCr can be efficiently computed using several approaches, including factorial computation, recursion, binomial coefficient formula, logarithmic formula, and an optimized method that eliminates redundant calculations. Each approach has its own time and space complexity considerations, with the optimized solution being particularly suitable for handling large values due to its linear time complexity and constant space complexity. Additionally, further exploration of dynamic programming techniques and space-time efficient binomial coefficient calculations offer more efficient solutions for handling complex scenarios ....

Contact Us