Medium · Combination Backtracking
Understand → Visualise → Practice.
Return every size-k combination selected from the integers 1 through n.
Combinations ignore order, so generate each one in increasing order: after choosing x, the next choice starts at x + 1.
start is the smallest number still allowed.
For x from start to n, append x, recurse with start = x + 1, then remove x.
When the path has k numbers, save a copy.
O(k × C(n, k)) time and O(k) recursion depth, plus the output.
Restarting each level from 1 creates the same combination in several orders.
Read the full guide first: Recursion and Backtracking.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.