Math Fundamentals18 sections · 814 units
Open in Course

Problem - Combinations

Generate C(n,k)

Given two integers nn and kk, return all possible combinations of kk numbers chosen from the range [1,n][1, n]. For example, if n=4n = 4 and k=2k = 2, you return [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]][[1,2], [1,3], [1,4], [2,3], [2,4], [3,4]].

This is the classic combinations problem. You learned C(n,k)=n!k!(nk)!C(n, k) = \frac{n!}{k!(n-k)!} counts how many combinations exist. Now you will generate them all.

The key constraint: order does not matter. [1,2][1, 2] and [2,1][2, 1] are the same combination. You need to avoid duplicates.