import java.util.*;
import java.lang.*;
import java.io.*;
class SubsetSum
{
public static <T> Set<Set<T>> powerSet(Set<T> originalSet)
{
Set<Set<T>> sets = new HashSet<Set<T>>();
if (originalSet.isEmpty())
{
sets.add(new HashSet<T>());
return sets;
}
List<T> list = new ArrayList<T>(originalSet);
T head = list.get(0);
Set<T> rest = new HashSet<T>(list.subList(1, list.size()));
for (Set<T> set : powerSet(rest))
{
Set<T> newSet = new HashSet<T>();
newSet.add(head);
newSet.addAll(set);
sets.add(newSet);
sets.add(set);
}
return sets;
}
public static void main
(String[] args
) {
Set<Integer> mySet = new HashSet<Integer>();
int[] arr={3, 5, 5, 7};
int target = 10;
int numVals = 4;
for(int i=0;i<numVals;++i)
{
mySet.add(i);
}
System.
out.
println("Solutions: "); for (Set<Integer> s : powerSet(mySet))
{
int sum = 0;
{
sum += arr[e];
}
if (sum == target)
{
{
soln += arr[e];
soln += " ";
}
soln += "]";
}
}
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgpjbGFzcyBTdWJzZXRTdW0KewoJcHVibGljIHN0YXRpYyA8VD4gU2V0PFNldDxUPj4gcG93ZXJTZXQoU2V0PFQ+IG9yaWdpbmFsU2V0KQoJewogICAgCVNldDxTZXQ8VD4+IHNldHMgPSBuZXcgSGFzaFNldDxTZXQ8VD4+KCk7CiAgICAJaWYgKG9yaWdpbmFsU2V0LmlzRW1wdHkoKSkgCiAgICAJewogICAgCQlzZXRzLmFkZChuZXcgSGFzaFNldDxUPigpKTsKICAgIAkJcmV0dXJuIHNldHM7CgkgCX0KICAgIAlMaXN0PFQ+IGxpc3QgPSBuZXcgQXJyYXlMaXN0PFQ+KG9yaWdpbmFsU2V0KTsKICAgIAlUIGhlYWQgPSBsaXN0LmdldCgwKTsKICAgIAlTZXQ8VD4gcmVzdCA9IG5ldyBIYXNoU2V0PFQ+KGxpc3Quc3ViTGlzdCgxLCBsaXN0LnNpemUoK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