Skip to content

Commit 8a6d19a

Browse files
authored
Create PowerSet.java
1 parent cef6ffe commit 8a6d19a

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Medium/PowerSet.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package AlgoExpert_Medium;
2+
3+
import java.util.List;
4+
import java.util.ArrayList;
5+
6+
public class PowerSet {
7+
8+
public static void main(String[] args) {
9+
List<Integer> array = new ArrayList<>() ;
10+
array.add(1) ; array.add(2) ; array.add(3) ;
11+
System.out.println( powerset( array ) ) ;
12+
}
13+
14+
public static List<List<Integer>> powerset(List<Integer> array) {
15+
16+
List<List<Integer>> list = new ArrayList<List<Integer>>() ;
17+
list.add(new ArrayList<>() ) ;
18+
19+
List<Integer> current = new ArrayList<>() ;
20+
21+
return pset(array, current, list) ;
22+
}
23+
24+
public static List<List<Integer>> pset ( List<Integer> array, List<Integer> current, List<List<Integer>> list ) {
25+
26+
27+
for(int i=0 ; i<array.size(); i++) {
28+
29+
int currentNumber = array.get(i) ;
30+
31+
if(!current.contains(currentNumber) ){ // Constraints.
32+
33+
if(current.size()<1 || currentNumber > current.get(current.size()-1) ) { // Nice use of ||
34+
current.add(currentNumber) ; // Choose
35+
list.add(new ArrayList<>(current)) ;
36+
pset(array, current, list) ; // Explore
37+
current.remove(current.size()-1) ; // Unchoose
38+
}
39+
}
40+
}
41+
42+
return list ;
43+
44+
}
45+
46+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy