-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathRecursion2DArraySubsets.cpp
More file actions
42 lines (33 loc) · 946 Bytes
/
Recursion2DArraySubsets.cpp
File metadata and controls
42 lines (33 loc) · 946 Bytes
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
//Form subsets containg the elements of the 2D input array and return them.
//Provided you mainatin the sequence of elemnts of the original input array
#include <bits/stdc++.h>
using namespace std;
int subset(int input[], int n, int output[][20]){
if(n<=0) {
output[0][0]=0;
return 1;
}
int smallOutput = subset(input+1,n-1,output);
for(int i=0;i<smallOutput;i++) {
int col = output[i][0] +1;
output[i+smallOutput][0] = col;
output[i+smallOutput][1] = input[0];
for(int j=2; j<col+1;j++) {
output[i+smallOutput][j] = output[i][j-1];
}
}
return 2*smallOutput;
}
int main() {
int input[20],length, output[1000][20];
cin >> length;
for(int i=0; i < length; i++)
cin >> input[i];
int size = subset(input, length, output);
for( int i = 0; i < size; i++) {
for( int j = 1; j <= output[i][0]; j++) {
cout << output[i][j] << " ";
}
cout << endl;
}
}