You can use recursion, loops, vectors or strings doesn't matter.
Given an input N will generate N lines of the pattern.
1
2
1 2
3
1 3
1 2 3
2 3
4
1 4
1 2 4
1 3 4
1 2 3 4
2 4
2 3 4
3 4
5
Edit: I figured out how to do it doing a pre-calculation. It is possible to do it iteratively one step at a time?
void per(vector<int> v, int value) {
cout << v;
if (v.size() == value) {
return;
}
if(v.back() < value && v.size() < value) {
vector<int> modified(v);
for(int i = 0; i < modified.size(); i++) {
modified[i] += 1;
}
per(modified, value);
}
if(v.back() < value && v.size() < value) {
// for(int i = 0; i < value; i++) {
vector<int> modified(v);
modified.push_back(modified.back() + 1);
per(modified, value);
}
}
How do you think about such a recursive algorithm. I tried to draw a tree of the output but get suck. Maybe such problem doesn't fit well with recursion. It seems like it should be recursive and there is a smallest subset of the problem contained within.