Combinations
Given two integers n and k, return all possible combinations of k numbers chosen from the range:
[1, n]
You may return the combinations in any order.
Each combination must contain exactly k distinct numbers.
Example 1
Input
n = 4 k = 2
Output
1 2 1 3 1 4 2 3 2 4 3 4
Explanation
We need to choose 2 distinct numbers from [1, 4].
The possible combinations are:
[1,2]
[1,3]
[1,4]
[2,3]
[2,4]
[3,4]
Example 2
Input
n = 5 k = 3
Output
1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5
Explanation
We choose exactly 3 distinct numbers from [1,5].
There are:
C(5,3) = 10
possible combinations.
Constraints
Hints:
Hint 1
Start with an empty combination and try choosing numbers from 1 to n.
Hint 2
After choosing a number, recursively choose the next number starting from the following number so that numbers are not repeated.
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 20, 2026
Last updated
Aug 21, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output