Get Subset From Set That Sum To A Value Using Java

This is beyond my intellect since I am just totally bad with Math so the only thing left for me to do was the scour the Internet to try and find if anyone posted a solution to getting a list of subset from a given set that sum up to a specific value.

I wanted it to be a simple static method but out of the possible solutions that I came across with, only the class made by user rolfl in the Code Review StackExchange website met my needs.

This class made it possible for me to use the class and loop through each set and see if any of those have numbers that sum up to the specific value that I want.

Here is the class.

To use the class. Do it like this:

The example looks for subsets of a given set that sum up to the value 15. The dummy variable is an ArrayList of type Integer.

Then, if any subsets are found within the set, the totalsubset variable will have a value of more than 0.

You can modify the class to suit your requirements like getting the list of subsets by modifying the code in the print() method.

Related Posts Plugin for WordPress, Blogger...

Leave a Reply

Your email address will not be published. Required fields are marked *