ExamAdda Logo

Combinations

Easy

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

1 <= n <= 20
1 <= k <= n
All numbers in a combination must be distinct.
Each combination must contain exactly k numbers.
Numbers are selected from the range [1
n].

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

ExamAdda Technical Review Team

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.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output