Binomial coefficient in Java Find nCr for given n and r.
  
  Input:
  
  First line contains no of test cases T, for every test case 2
  integers as inputs (n,r).
  
  Output:
  
  Compute and print the value in separate line. Modulus your
  output to 10^9+7. If n
  
  Constraints: 
  
  1&lt;=T&lt;=50 
  
  1&lt;=n&lt;=1000
  
  1&lt;=r&lt;=800
  
  Example:
  
  Input: 
  
  1 
  
  3 2
  
  Output: 
  
  3


My approach:

import java.util.Scanner;
import java.lang.Math;
import java.math.BigInteger;

class GFG {

    private static int calcBinCoeff (int num, int r) {

        BigInteger numerator = new BigInteger("1");
        int limit = (int)(Math.pow(10,9) + 7);
        BigInteger modulus = new BigInteger(String.valueOf(limit));

        for (int i = num; i &gt; num - r ; i--) {
            BigInteger ind = new BigInteger(String.valueOf(i));
            numerator = numerator.multiply(ind);
        }   

        BigInteger fact = new BigInteger("1");

        for (int i = 2; i &lt;= r; i++) {
            BigInteger elem = new BigInteger(String.valueOf(i));
            fact = fact.multiply(elem);
        }

        BigInteger ans = (numerator.divide(fact)).mod(modulus);

        return ans.intValue();
    }

    public static void main (String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            int numTests = sc.nextInt();

            while (numTests-- &gt; 0) {
                int num = sc.nextInt();
                int r = sc.nextInt();

                System.out.println(calcBinCoeff(num, r));
            }
        }

    }
}


I have the following questions with regards to the above code:

1.Is there a smarter way to approach this question?


How can I improve the space and time complexity of the given question?
Have I gone too overboard by using BigInteger Library for this question?


Reference
Is there a smarter way to approach this question?


We'll see in a moment. Parts of it can be improved for sure.


How can I improve the space and time complexity of the given question?


You can't. The complexity will remain the same. The changes you can make might give small increases in performance or decrease memory usage, but the complexity will remain the same.


Have I gone too overboard by using BigInteger Library for this question?


I thought at first that it was possible to use just long, but I'm afraid not. Because of 1000 nCr 500, and such big numbers, you are stuck with BigInteger.



You can use BigInteger.valueOf instead of using the constructor with a String.

You can save some time by using the symmetric property of nCr if r > n / 2. For example, 100 nCr 80 is the same as 100 nCr 20.

You can use just one for-loop instead of two:

value = 1;
for (int i = 0; i &lt; r; i++) {
    value = value * (n - i) / (i + 1);
}
return value;


I used something similar in a project of mine (where I used double instead and thus didn't have to think about truncating integers - an issue which is fixed above thanks to Martin R).