Open In App

Make all combinations of size k

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 the 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 all remaining combination.

C++

 `// C++ program to print all combinations of size``// k of elements in set 1..n``#include ``using` `namespace` `std;` `void` `makeCombiUtil(vector >& 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 > makeCombi(``int` `n, ``int` `k)``{``    ``vector > 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 > 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;``}`

Java

 `// Java program to print all combinations of size``// k of elements in set 1..n``import` `java.util.*;``public` `class` `Main``{``    ``static` `Vector> ans = ``new` `Vector>();``    ``static` `Vector tmp = ``new` `Vector();``      ` `    ``static` `void` `makeCombiUtil(``int` `n, ``int` `left, ``int` `k)``    ``{``      ` `        ``// Pushing this vector to a vector of vector``        ``if` `(k == ``0``) {``            ``ans.add(tmp);``            ``for``(``int` `i = ``0``; i < tmp.size(); i++)``            ``{``                ``System.out.print(tmp.get(i) + ``" "``);``            ``}``            ``System.out.println();``            ``return``;``        ``}`` ` `        ``// i iterates from left to n. First time``        ``// left will be 1``        ``for` `(``int` `i = left; i <= n; ++i)``        ``{``            ``tmp.add(i);``            ``makeCombiUtil(n, i + ``1``, k - ``1``);`` ` `            ``// Popping out last inserted element``            ``// from the vector``            ``tmp.remove(tmp.size() - ``1``);``        ``}``    ``}`` ` `    ``// Prints all combinations of size k of numbers``    ``// from 1 to n.``    ``static` `Vector> makeCombi(``int` `n, ``int` `k)``    ``{``        ``makeCombiUtil(n, ``1``, k);``        ``return` `ans;``    ``}``    ` `    ``public` `static` `void` `main(String[] args)``    ``{``      ` `        ``// given number``        ``int` `n = ``5``;``        ``int` `k = ``3``;``        ``ans = makeCombi(n, k);``    ``}``}` `// This code is contributed by suresh07.`

Python3

 `# Python3 program to print all combinations of size``# k of elements in set 1..n``ans ``=` `[]``tmp ``=` `[]` `def` `makeCombiUtil(n, left, k):``    ``# Pushing this vector to a vector of vector``    ``if` `(k ``=``=` `0``):``        ``ans.append(tmp)``        ``for` `i ``in` `range``(``len``(tmp)):``            ``print``(tmp[i], end ``=` `" "``)``        ``print``()``        ``return` `    ``# i iterates from left to n. First time``    ``# left will be 1``    ``for` `i ``in` `range``(left, n ``+` `1``):``        ``tmp.append(i)``        ``makeCombiUtil(n, i ``+` `1``, k ``-` `1``)` `        ``# Popping out last inserted element``        ``# from the vector``        ``tmp.pop()` `# Prints all combinations of size k of numbers``# from 1 to n.``def` `makeCombi(n, k):``    ``makeCombiUtil(n, ``1``, k)``    ``return` `ans`` ` `# given number``n ``=` `5``k ``=` `3``ans ``=` `makeCombi(n, k)` `# This code is contributed by divyeshrabadiya07.`

C#

 `// C# program to print all combinations of size``// k of elements in set 1..n``using` `System;``using` `System.Collections.Generic;``class` `GFG {``    ` `    ``static` `List> ans = ``new` `List>();``    ``static` `List<``int``> tmp = ``new` `List<``int``>();``      ` `    ``static` `void` `makeCombiUtil(``int` `n, ``int` `left, ``int` `k)``    ``{``      ` `        ``// Pushing this vector to a vector of vector``        ``if` `(k == 0) {``            ``ans.Add(tmp);``            ``for``(``int` `i = 0; i < tmp.Count; i++)``            ``{``                ``Console.Write(tmp[i] + ``" "``);``            ``}``            ``Console.WriteLine();``            ``return``;``        ``}`` ` `        ``// i iterates from left to n. First time``        ``// left will be 1``        ``for` `(``int` `i = left; i <= n; ++i)``        ``{``            ``tmp.Add(i);``            ``makeCombiUtil(n, i + 1, k - 1);`` ` `            ``// Popping out last inserted element``            ``// from the vector``            ``tmp.RemoveAt(tmp.Count - 1);``        ``}``    ``}`` ` `    ``// Prints all combinations of size k of numbers``    ``// from 1 to n.``    ``static` `List> makeCombi(``int` `n, ``int` `k)``    ``{``        ``makeCombiUtil(n, 1, k);``        ``return` `ans;``    ``}` `  ``static` `void` `Main()``  ``{``    ` `    ``// given number``    ``int` `n = 5;``    ``int` `k = 3;``    ``ans = makeCombi(n, k);``  ``}``}` `// This code is contributed by rameshtravel07.`

Javascript

 ``

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 ```

Time Complexity:  O((nCk)*k), where nCk is all possible subsets and k to copy subsets into ans vector.

Space Complexity: O((nCk)*k), to store all n C k subset in the ans vector of size k.

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