logo CodeStepByStep logo


Language/Type: C++ recursion return
Related Links:
Author: Marty Stepp (on 10/12)

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).

Type your C++ solution code here:

This is a function problem. Write a C++ function as described. Do not write a complete program; just the function(s) above.

You must log in before you can solve this problem.

Log In

Need help?

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.