Skip to content
Onsite

Combinations

MediumP2Coding

Return all combinations of k numbers chosen from 1..n.

Write each combination in increasing order and list the combinations in lexicographic order, for example [1,2] before [1,3].

Example 1

Input
n = 4, k = 2
Output
[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
Why
Six combinations of 2 numbers out of 4.

Example 2

Input
n = 1, k = 1
Output
[[1]]

Example 3

Input
n = 3, k = 3
Output
[[1,2,3]]

Constraints

  • 1 <= n <= 20
  • 1 <= k <= n
  • The number of combinations is at most 200000.

Hints

Run checks the 3 visible tests, Submit all 9.