logo CodeStepByStep logo

combin

Write a method named combin that uses dynamic programming to compute binomial coefficients, otherwise known as "n choose k." Your method accepts two integer parameters n and k and returns a long representing the number of unique combinations of k values taken from a set of n values. For example, the call of combin(7, 4) should return 35. Return your answer as a long long int, which is a 64-bit integer with a larger range, so that you can handle large values of n and k.

If k is equal to 0 or n, return 1. Otherwise, if there are no values to choose from (if n is zero or negative), or if k is negative, or if k exceeds n, return 0.

The key constraint of this problem is that you must solve it using a bottom-up dynamic programming approach. Do not use recursion. You are allowed to construct any data structures (array, list, set, map, etc.) necessary to store the data for your dynamic programming algorithm.

You may assume that the values passed for n and k will be non-negative and that the number of combinations fits in the range of type long (will not overflow).

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.