data-structures · intermediate · ~15 min
Compute C(n,k) exactly.
Implement:
long long choose(int n, int k);
Return C(n,k) = n!/(k!(n-k)!). Return 0 if k<0 or k>n. 0 <= n <= 62.
n in [0,62], any k.
C(n,k), or 0 if out of range.
Avoid overflow by building the product incrementally.
#include <stddef.h>
/* Binomial coefficient C(n,k) = n!/(k!(n-k)!). Return 0 if k<0 or k>n. 0<=n<=62. */
long long choose(int n,int k){ (void)n;(void)k; return 0; }
Naive Pascal recursion is exponential; overflow if you compute n! directly.
C(n,0)=C(n,n)=1.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.