Created
August 5, 2011 08:49
-
-
Save mbykovskyy/1127147 to your computer and use it in GitHub Desktop.
An algorithm for generating all possible combinations of sums of elements.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<Integer> sums(int sum, int offset, int[] array) { | |
List<Integer> sums = new ArrayList<Integer>(array.length - offset); | |
for (int i = offset; i < array.length; ++i) { | |
int total = sum + array[i]; | |
sums.add(total); | |
sums.addAll(sums(total, i + 1, array)); | |
} | |
return sums; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment