Learn
Exam

Combinations

Difficulty
INTERMEDIATE

Problem

Given two natural numbers n and m, write a solution function that returns all sets of m numbers selected from the integers between 1 and n that satisfy the following conditions:

Conditions:

  • Each set contains exactly A1,A2,⋯ ,AmA_1, A_2, \cdots, A_m numbers.
  • 1≤A1<A2<⋯<Am≤n1 \leq A_1 < A_2 < \cdots < A_m \leq n.
  • If multiple sets satisfy the above conditions, return the sets in lexicographical order.

Constraints

  • n is a natural number less than or equal to 15.
  • m is a natural number less than or equal to n.

Test Case

Test Case Number n m return
1 3 2 [[1,2],[1,3],[2,3]]
2 5 1 [[1],[2],[3],[4],[5]]
3 5 5 [[1,2,3,4,5]]

Explanation for Test Case 1

  • From the integers 1, 2, and 3, selecting and arranging 2 numbers in ascending order produces the following 3 sets: [1,2], [1,3], [2,3].
  • Sorting these sets lexicographically results in [[1,2],[1,3],[2,3]].

Explanation for Test Case 2

  • From the integers 1, 2, 3, 4, and 5, selecting and arranging 1 number in ascending order produces the following 5 sets: [1], [2], [3], [4], [5].
  • Sorting these sets lexicographically results in [[1],[2],[3],[4],[5]].

Explanation for Test Case 3

  • From the integers 1, 2, 3, 4, and 5, selecting and arranging all 5 numbers in ascending order produces a single set: [1,2,3,4,5].
  • Sorting these sets lexicographically results in [[1,2,3,4,5]].