-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprint_subset_array.cpp
More file actions
50 lines (44 loc) · 1.2 KB
/
print_subset_array.cpp
File metadata and controls
50 lines (44 loc) · 1.2 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
/* Q-->
Given an integer array (of length n), find and print all the subsets of input array.
Subsets are of length varying from 0 to n, that contain elements of the array. But the order of elements should remain same as in the input array.
Note : The order of subsets are not important. Just print the subsets in different lines.
Input format :
Line 1 : Integer n, Size of array
Line 2 : Array elements (separated by space)
Constraints :
1 <= n <= 15
Sample Input:
3
15 20 12
Sample Output:
""
12
20
20 12
15
15 12
15 20
15 20 12
*/
void printSubsetsOfArray(int input[],int size, int output[],int outputSize){
if(size == 0){
for(int i=0;i<outputSize;i++){
cout<<output[i]<<" ";
}
cout<<endl;
return;
}
printSubsetsOfArray(input+1,size-1,output,outputSize);
//assuming current elemnet is included in array
int newoutput[20] = {};
for(int i=0;i<outputSize;i++){
newoutput[i] = output[i];
}
newoutput[outputSize] = input[0];
printSubsetsOfArray(input+1,size-1,newoutput,outputSize+1);
}
void printSubsetsOfArray(int input[], int size) {
int ans[20] = {};
printSubsetsOfArray(input,size,ans,0);
}
//write main yourself