Skip to content

Instantly share code, notes, and snippets.

@mbykovskyy
Created August 5, 2011 08:49
Show Gist options
  • Save mbykovskyy/1127147 to your computer and use it in GitHub Desktop.
Save mbykovskyy/1127147 to your computer and use it in GitHub Desktop.
An algorithm for generating all possible combinations of sums of elements.
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