-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram74.java
More file actions
32 lines (27 loc) · 879 Bytes
/
Program74.java
File metadata and controls
32 lines (27 loc) · 879 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
import java.util.ArrayList;
public class Program74 {
//print all the subsets of a set of first n natural numbers --> n=3
public static void printSubset(ArrayList<Integer> subset) {
for (int i = 0; i < subset.size(); i++) {
System.out.print(subset.get(i) + " ");
}
System.out.println(" ");
}
public static void findSubsets(int n, ArrayList<Integer> subset) {
if (n == 0) {
printSubset(subset);
return;
}
//add element
subset.add(n);
findSubsets(n - 1, subset);
//dont add element
subset.remove(subset.size() - 1);
findSubsets(n - 1, subset);
}
public static void main(String[] args) {
int n = 3;
ArrayList<Integer> subset = new ArrayList<>();
findSubsets(n, subset);
}
}