fork(2) download
  1. import java.util.ArrayList;
  2. class PowerSet
  3. {
  4. public static ArrayList<String> getpowerset(int a[],int n,ArrayList<String> ps)
  5. {
  6. if(n<0)
  7. {
  8. return null;
  9. }
  10. if(n==0)
  11. {
  12. if(ps==null)
  13. ps=new ArrayList();
  14. ps.add(" ");
  15. return ps;
  16. }
  17. ps=getpowerset(a, n-1, ps);
  18. ArrayList<String> tmp=new ArrayList<String>();
  19. for(String s:ps)
  20. {
  21. if(s.equals(" "))
  22. tmp.add(""+a[n-1]);
  23. else
  24. tmp.add(s+a[n-1]);
  25. }
  26. ps.addAll(tmp);
  27. return ps;
  28. }
  29. public static void main(String[] ar)
  30. {
  31. int a[]={1,2,3,4};
  32. int n=a.length;
  33. ArrayList<String> subset=PowerSet.getpowerset(a, n, new ArrayList<String>());
  34.  
  35. for(String s:subset)
  36. System.out.println("{"+s+"}");
  37. }
  38. }
  39.  
stdin
Standard input is empty
compilation info
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
stdout
{ }
{1}
{2}
{12}
{3}
{13}
{23}
{123}
{4}
{14}
{24}
{124}
{34}
{134}
{234}
{1234}