LeetCode 77, Medium. Topics: Backtracking. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 15 parametrized pytest cases, and a playground notebook:
Problem
Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n].
You may return the answer in any order.
Examples
Explanation: There are 4 choose 2 = 6 total combinations.
Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination.
Explanation: There is 1 choose 1 = 1 total combination.
Constraints
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode 250, NeetCode All. Last modified on September 7, 2026