data-structures · intermediate · ~15 min

k-permutations P(n,k)

Count ordered arrangements recursively.

Challenge

Implement:

long long permutations_nk(int n, int k);

Return the number of ordered arrangements of k items chosen from n, P(n,k) = n!/(n-k)!, computed recursively. 0 <= k <= n <= 20.

Input format

k <= n <= 20.

Output format

P(n,k).

Constraints

Multiply the top k factors.

Starter code

#include <stddef.h>
/* Number of ordered arrangements of k items chosen from n, P(n,k)=n!/(n-k)!, recursively. 0<=k<=n<=20. */
long long permutations_nk(int n,int k){ (void)n;(void)k; return 0; }

Common mistakes

Confusing with combinations (order matters here).

Edge cases to handle

P(n,0)=1; P(n,n)=n!.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.