The Wayback Machine - https://web.archive.org/web/20240720173230/https://www.geeksforgeeks.org/program-to-calculate-value-of-ncr-using-recursion/
Open In App

Program to calculate value of nCr using Recursion

Last Updated : 12 Jan, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Given two numbers N and r, find the value of NCr using recursion

Examples:

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

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

 

Approach 1: One very interesting way to find the C(n,r) is the recursive method which is based on the recursive equation.

C(n,r) = C(n-1,r-1) + C(n-1,r)

Below is the implementation of the above approach as follows:

C++




#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the value of nCr
// using recursion
 
int comb(int n,int r){
   if(n<r){
       return 0;
   }
   if(r == 0){
       return 1;
   }
   if(r == 1){
       return n;
   }
   if(n == 1){
       return 1;
   }
   return comb(n-1,r-1)+comb(n-1,r);
}
 
// Driver code
int main(){
   int n = 10,r = 5;
   cout << comb(n,r);
}


Java




// Java code for the above approach
import java.io.*;
class GFG {
 
  // Function to calculate the value of nCr
  // using recursion
  static int comb(int n, int r)
  {
     if(n<r){
       return 0;
     }
     if(r == 0){
         return 1;
     }
     if(r == 1){
         return n;
     }
     if(n == 1){
         return 1;
     }
     return comb(n-1,r-1)+comb(n-1,r);
  }
 
  public static void main(String[] args)
  {
    int n = 5, r = 3;
 
    System.out.println(comb(n, r));
  }
}


Python3




# Python code to implement above approach
 
# Function to calculate the value of nCr
# using recursion
 
def comb(n, r):
    if(n < r):
        return 0
    if(r == 0):
        return 1
    if(r == 1):
        return n
    if(n == 1):
        return 1
    return comb(n - 1, r - 1) + comb(n - 1, r)
     
#  Driver code
n = 10
r = 5
print(comb(n, r))
 
# This code is contributed by Pushpesh Raj.


C#




using System;
 
public class GFG{
 
  // Function to calculate the value of nCr
  // using recursion
  static int comb(int n, int r)
  {
    if(n<r){
       return 0;
    }
    if(r == 0){
       return 1;
    }
    if(r == 1){
       return n;
    }
    if(n == 1){
       return 1;
    }
    return comb(n-1,r-1)+comb(n-1,r);
  }
 
  // Driver code
  static public void Main (){
 
    int n = 5, r = 3;
    Console.WriteLine(comb(n, r));
  }
}


Javascript




<script>
//Javascript code to implement above approach
 
// Function to calculate the value of nCr
// using recursion
 
function comb(n, r){
   if( n<r ){
       return 0;
   }
   if(r == 0){
       return 1;
   }
   if(r == 1){
       return n;
   }
   if(n == 1){
       return 1;
   }
   return comb(n-1,r-1) + comb(n-1,r);
}
 
// Driver code
 
let n = 5, r = 3;
document.write(comb(n,r));
  
// </script>


Output

252

Time Complexity: O(2^n), Auxiliary Space: O(n)

Approach 2: Another idea is simply based on the below formula.

NCr = N! / (r! * (N-r)!)
Also, 
NCr-1 = N! / ( (r-1)! * (N – (r-1))! )

Hence,

  • NCr * r! * (N – r)! = NCr-1  * (r-1)! * (N – (r-1))!
  • NCr * r * (N-r)! = NCr-1  * (N-r+1)!                          [eliminating (r – 1)! from both side]
  • NCr * r = nCr-1  * (N-r+1)

Therefore,

NCr = NCr-1 * (N-r+1) / r

Below is the implementation of the above approach:

C++




// C++ code to implement above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the value of nCr
// using recursion
int nCr(int N, int r)
{
    int res = 0;
    if (r == 0) {
        return 1;
    }
    else {
        res = nCr(N, r - 1)
              * (N - r + 1) / r;
    }
    return res;
}
 
// Driver code
int main()
{
    int N = 5, r = 3;
    cout << nCr(N, r);
    return 0;
}


Java




// Java code for the above approach
import java.io.*;
class GFG {
 
  // Function to calculate the value of nCr
  // using recursion
  static int nCr(int N, int r)
  {
    int res = 0;
    if (r == 0) {
      return 1;
    }
    else {
      res = nCr(N, r - 1) * (N - r + 1) / r;
    }
    return res;
  }
 
  public static void main(String[] args)
  {
    int N = 5, r = 3;
 
    System.out.println(nCr(N, r));
  }
}
 
// This code is contributed by Potta Lokesh


Python3




# Python code to implement above approach
 
# Function to calculate the value Of nCr
# using recursion
def nCr(N, r):
    res = 0
    if(r == 0):
        return 1
    else:
        res = nCr(N, r-1)
* (N-r + 1) / r
    return res
 
 
# Driver code
if __name__ == "__main__":
    N = 5
    r = 3
    print(int(nCr(N, r)))


C#




using System;
 
public class GFG{
 
  // Function to calculate the value of nCr
  // using recursion
  static int nCr(int N, int r)
  {
    int res = 0;
    if (r == 0) {
      return 1;
    }
    else {
      res = nCr(N, r - 1)
        * (N - r + 1) / r;
    }
    return res;
  }
 
  // Driver code
  static public void Main (){
 
    int N = 5, r = 3;
    Console.WriteLine(nCr(N, r));
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript




<script>
//Javascript code to implement above approach
 
// Function to calculate the value of nCr
// using recursion
function nCr(N, r)
{
    let res = 0;
    if (r == 0) {
        return 1;
    }
    else {
        res = nCr(N, r - 1)
              * (N - r + 1) / r;
    }
    return res;
}
 
// Driver code
 
let N = 5, r = 3;
document.write(nCr(N,r));
     
// This code is contributed by Taranpreet
// </script>


Output

10

Time Complexity: O(r), Auxiliary Space: O(r)

Complexity Analysis: 
The time complexity of the above approach is O(r). This is because the function makes a single recursive call for each value of r, and the time taken to calculate the value of nCr is constant.

The Auxiliary space complexity of the above approach is O(r), as the recursive function calls create a new stack frame for each call. This means that the program will consume a significant amount of memory for larger values of r.



Previous Article
Next Article

Similar Reads

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!) represen
15+ min read
Calculate nCr using Pascal's Triangle
A useful application of Pascal's triangle is the calculation of combinations. The formula to find nCr is n! / r! * (n - r)! which is also the formula for a cell of Pascal's triangle. Pascal's triangle: Input: n = 5, r = 3Output: 10Explanation:n! / r! * (n - r)! = 5! / 3! * (2!) = 120 / 12 = 10Input: n = 7, r = 2Output: 21Explanation:n! / r! * (n -
11 min read
Find a pair from the given array with maximum nCr value
Given an array arr[] of n positive integers. The task is to find elements arr[i] and arr[j] from the array such that arr[i]Carr[j] is maximum possible. In case of more than 1 valid pairs, print any one of them.Examples: Input: arr[] = {3, 1, 2} Output: 3 2 3C1 = 3 3C2 = 3 2C1 = 2 (3, 1) and (3, 2) are the only pairs with maximum nCr.Input: arr[] =
14 min read
Find a pair (n,r) in an integer array such that value of nCr is maximum
Given an array of non-negative integers arr[]. The task is to find a pair (n, r) such that value of nCr is maximum possible r &lt; n. nCr = n! / (r! * (n - r)!) Examples: Input: arr[] = {5, 2, 3, 4, 1} Output: n = 5 and r = 2 5C3 = 5! / (3! * (5 - 3)!) = 10Input: arr[] = {0, 2, 3, 4, 1, 6, 8, 9} Output: n = 9 and r = 4 Naive approach: A simple appr
7 min read
Program to Calculate e^x by Recursion ( using Taylor Series )
The value of the Exponential function can be calculated using Taylor Series. [Tex]e^x[/Tex] = 1 + x/1! + [Tex]x^2[/Tex]/2! + [Tex]x^3[/Tex]/3! + ...... + until n termsAs the number of terms increases the more precise value of ex is obtained. To find e^x using the recursive function, we need to use static variables. A function can return only one va
5 min read
Why is Tail Recursion optimization faster than normal Recursion?
What is tail recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call. What is non-tail recursion? Non-tail or head recursion is defined as a recursive function in which the recursive call is the f
4 min read
Write a program to Calculate Size of a tree | Recursion
Size of a tree is the number of elements present in the tree. Size of the below tree is 5. Size() function recursively calculates the size of a tree. It works as follows:Size of a tree = Size of left subtree + 1 + Size of right subtree. Recommended PracticeSize of Binary TreeTry It!Algorithm: size(tree)1. If tree is empty then return 02. Else (a) G
14 min read
Compute nCr%p using Lucas Theorem
Given three numbers n, r and p, compute the value of nCr mod p. Examples: Input: n = 10, r = 2, p = 13 Output: 6 Explanation: 10C2 is 45 and 45 % 13 is 6. Input: n = 1000, r = 900, p = 13 Output: 8 We strongly recommend referring below post as a prerequisite of this.Compute nCr % p | Set 1 (Introduction and Dynamic Programming Solution)We have intr
12 min read
Compute nCr%p using Fermat Little Theorem
Given three numbers n, r and p, compute the value of nCr mod p. Here p is a prime number greater than n. Here nCr is Binomial Coefficient.Example: Input: n = 10, r = 2, p = 13 Output: 6 Explanation: 10C2 is 45 and 45 % 13 is 6. Input: n = 6, r = 2, p = 13 Output: 2Recommended PracticenCrTry It! We have discussed the following methods in previous po
15+ min read
Sum of product of r and rth Binomial Coefficient (r * nCr)
Given a positive integer n. The task is to find the sum of the product of r and rth Binomial Coefficient. In other words find: ? (r * nCr), where 0 &lt;= r &lt;= n.Examples: Input : n = 2 Output : 4 0.2C0 + 1.2C1 + 2.2C2 = 0*2 + 1*2 + 2*1 = 4 Input : n = 5 Output : 80 Method 1 (Brute Force) : The idea is to iterate a loop i from 0 to n and evaluate
9 min read
three90RightbarBannerImg