-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsets.java
More file actions
38 lines (30 loc) · 1.34 KB
/
Copy pathSubsets.java
File metadata and controls
38 lines (30 loc) · 1.34 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
package Recursion.String;
import java.util.ArrayList;
import java.util.List;
public class Subsets {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
List<List<Integer>> ans = subsets(arr);
for (List<Integer> list : ans){ // this will print all the smaller List which is a part of bigger ans list.
System.out.println(list);
}
}
static List<List<Integer>> subsets( int[] arr){
List<List<Integer>> outer = new ArrayList<>();
outer.add(new ArrayList<>());
for (int num : arr){
int n = outer.size(); // n is the size of the outer array.
for (int i = 0; i < n; i++) {
List<Integer> internal = new ArrayList<>(outer.get(i)); // now the internal part is the copy of the outer part array
// and now we just needed to add that element into it
internal.add(num); // now we are adding number to that internal array
// which is the copy of the outer array
// which is the part of the whole bigger array both outer and internal.
outer.add(internal); // now we have added that internal part in the outer part array
// which becomes whole array
// this is in a loop bcz
}
}
return outer;
}
}