logo CodeStepByStep logo

combin

Language/Type: C++ recursion return
Related Links:

Write a recursive function named combin that accepts two integer parameters n and k and returns "n choose k," which is 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.

To avoid excessive recursive calls, you must use memoization to cache previously computed results to speed up your function. If you do not memoize, your test results will time out.

You may assume that the number of combinations fits in the range of type long long int (will not overflow).

Function: Write a C++ function as described, not a complete program.

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.