Binomial coefficient in Java
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).