Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Combinations

Recursion & Backtrackingmedium

Implement `combine(n, k)` returning all `k`-length combinations of the numbers `1..n`, in standard backtracking order.

Sample tests

Input: combine(4, 2)
Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
Input: combine(1, 1)
Output: [[1]]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Allowing earlier indices again produces duplicates.

Learning resources

  • Wikipedia: Backtracking
Approach & explanation (try first)

Backtracking with a rising start index yields all k-combinations.

Loading...
⌘/Ctrl + Enter

Run your code to see results.