-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubsetGenerator.java
26 lines (26 loc) · 968 Bytes
/
SubsetGenerator.java
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
import java.util.*;
public class SubsetGenerator {
public static void generateSubsets(int[] set, int index, List<Integer> currentSubset) {
if (index == set.length) {
System.out.println(currentSubset);
return;
}
generateSubsets(set, index + 1, currentSubset);
currentSubset.add(set[index]);
generateSubsets(set, index + 1, currentSubset);
currentSubset.remove(currentSubset.size() - 1);
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter the number of elements in the set: ");
int n = s.nextInt();
int[] set = new int[n];
System.out.println("Enter the elements of the set:");
for (int i = 0; i < n; i++) {
set[i] = s.nextInt();
}
System.out.println("All subsets of the set:");
generateSubsets(set, 0, new ArrayList<>());
s.close();
}
}