Java Program to Perform nCr Combinations



In this article, we'll learn to perform nCr (Combinations) in Java. In mathematics, nCr (combinations) is a crucial concept that helps determine how many ways you can choose r items from a set of n items, without considering the selection order. It is widely used in fields like probability, statistics, and combinatorics

What is nCr?

The formula for calculating combinations (nCr) is ?

nCr = n! / (r! × (n - r)!)

Where,

  • n! = The factorial of n (product of all integers from 1 to n).
  • r! = The factorial of r.
  • (n - r)! = The factorial of the difference between n and r.

This formula calculates how many ways you can choose r items from a total of n items, without considering the selection order.

Using Factorials

This approach involves hardcoding the values of n and r. It uses factorial calculations to compute nCr based on the mathematical formula:

  • The factorial values of n, r, and (n-r) are calculated using the my_factorial method.
  • The nCr value is computed using the combination formula. 
  • Predefined values n = 6 and r = 4 are used for demonstration. 

Example

Below is an exampleto perform nCr (rcombinations) using factorials ?

public class Combination {
   // Function to compute nCr
   static int Compute_nCr(int n, int r) {
      return my_factorial(n) / (my_factorial(r) * my_factorial(n - r));
   }
   // Function to compute factorial
   static int my_factorial(int n) {
      int result = 1;
      for (int i = 2; i <= n; i++) {
         result *= i;
      }
      return result;
   }
   public static void main(String[] args) {
      int n = 6, r = 4; // Predefined values
      System.out.println("The values of n and r are " + n + " and " + r);
      System.out.println("The combination value (nCr) is = " + Compute_nCr(n, r));
   }
}

Output

The values of n and r are 6 and 4
The combination value (nCr) is = 15

Time Complexity: O(n) Computing factorials involves iterating up to n, r, and n?r..
Space Complexity: O(1) Only a constant amount of memory is used for variables

Using Iterative Logic Without Factorials

This approach directly calculates nCr using an iterative method. Instead of computing factorials, it simplifies the formula to avoid large intermediate values, especially when dealing with large n.

  • The symmetry property nCr=nC(n?r)nCr = nC(n-r) reduces unnecessary computations when r>n/2r > n/2.
  • The combination formula is computed iteratively:
    • Numerator: n×(n?1)×?n \times (n-1) 
    • Denominator: r!=r×(r?1)×?r! = r \times (r-1)
  • The iterative method avoids large factorials, making it efficient for larger values of n.

Example

Below is an example to perform nCr (rcombinations) using Iterative Logic ?

public class Combination {
   // Function to compute nCr iteratively
   static int Compute_nCr(int n, int r) {
      if (r > n - r) 
         r = n - r; // Use symmetry property nCr = nC(n-r)
      
      int result = 1;
      for (int i = 0; i < r; i++) {
         result = result * (n - i) / (i + 1);
      }
      return result;
   }


   public static void main(String[] args) {
      int n = 6, r = 4; // Predefined values
      System.out.println("The values of n and r are " + n + " and " + r);
      System.out.println("The combination value (nCr) is = " + Compute_nCr(n, r));
   }
}

Output

The values of n and r are 6 and 4
The combination value (nCr) is = 15

Time Complexity: O(r) The iterative computation runs for r steps, which is typically smaller than n or n?r.
Space Complexity: O(1) Like Approach 1, only a constant number of variables are used without extra memory allocation.

Comparison of Both Approaches

Aspect
Using Factorials
Using Iterative Logic
Efficiency
May struggle with large n due to factorial calculations.
Efficient for larger values of n and r.
Complexity
Uses multiple factorial computations.
Optimized with fewer computations.
Use Case
Suitable for smaller values of n and r.
Ideal for larger combinatorial calculations.

Conclusion

Both approaches provide effective methods for calculating nCr in Java: Use Approach 1 for simple cases where factorial calculations suffice.Opt for Approach 2 for performance-critical scenarios, especially when n or r is large

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2024-12-27T19:16:15+05:30

757 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements