Given two numbers n and k and you have to find all possible combination of k numbers from 1…n.

Examples:

Input : n = 4 k = 2 Output : 1 2 1 3 1 4 2 3 2 4 3 4 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

We have discussed one approach in below post.

Print all possible combinations of r elements in a given array of size n

In this, we use DFS based approach. We want all numbers from 1 to n. We first push all numbers from 1 to k in tmp_vector and as soon as k is equal to 0, we push all numbers from tmp_vector to ans_vector. After this we remove the last element from tmp_vector and make make all remaining combination.

`// C++ program to print all combinations of size ` `// k of elements in set 1..n ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `makeCombiUtil(vector<vector<` `int` `> >& ans, ` ` ` `vector<` `int` `>& tmp, ` `int` `n, ` `int` `left, ` `int` `k) ` `{ ` ` ` `// Pushing this vector to a vector of vector ` ` ` `if` `(k == 0) { ` ` ` `ans.push_back(tmp); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// i iterates from left to n. First time ` ` ` `// left will be 1 ` ` ` `for` `(` `int` `i = left; i <= n; ++i) ` ` ` `{ ` ` ` `tmp.push_back(i); ` ` ` `makeCombiUtil(ans, tmp, n, i + 1, k - 1); ` ` ` ` ` `// Popping out last inserted element ` ` ` `// from the vector ` ` ` `tmp.pop_back(); ` ` ` `} ` `} ` ` ` `// Prints all combinations of size k of numbers ` `// from 1 to n. ` `vector<vector<` `int` `> > makeCombi(` `int` `n, ` `int` `k) ` `{ ` ` ` `vector<vector<` `int` `> > ans; ` ` ` `vector<` `int` `> tmp; ` ` ` `makeCombiUtil(ans, tmp, n, 1, k); ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// given number ` ` ` `int` `n = 5; ` ` ` `int` `k = 3; ` ` ` `vector<vector<` `int` `> > ans = makeCombi(n, k); ` ` ` `for` `(` `int` `i = 0; i < ans.size(); i++) { ` ` ` `for` `(` `int` `j = 0; j < ans[i].size(); j++) { ` ` ` `cout << ans.at(i).at(j) << ` `" "` `; ` ` ` `} ` ` ` `cout << endl; ` ` ` `} ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

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

This article is contributed by **Roshni Agarwal**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- itertools.combinations() module in Python to print all possible combinations
- Print all the combinations of N elements by changing sign such that their sum is divisible by M
- Iterating over all possible combinations in an Array using Bits
- Iterative approach to print all combinations of an Array
- Combinations with repetitions
- Problem on permutations and combinations | Set 2
- Combinations from n arrays picking one element from each array
- Combinations in a String of Digits
- Print all subsets of given size of a set
- Python program to get all subsets of given size of a set
- Product of all Subsequences of size K except the minimum and maximum Elements
- Product of all sorted subsets of size K using elements whose index divide K completely
- Number of loops of size k starting from a specific node
- Number of arrays of size N whose elements are positive integers and sum is K
- Count of sub-sets of size n with total element sum divisible by 3
- Find number of ways to form sets from N distinct things with no set of size A or B
- Count non decreasing subarrays of size N from N Natural numbers
- Total number of Subsets of size at most K
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Largest component size in a graph formed by connecting non-co-prime nodes