-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnikhil.cpp
More file actions
54 lines (49 loc) · 1.1 KB
/
nikhil.cpp
File metadata and controls
54 lines (49 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#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;
int k;
cin >> n >> k;
'' 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;
}