Skip to content

Instantly share code, notes, and snippets.

@prophile
Forked from anonymous/gist:646264
Created October 26, 2010 03:52
Show Gist options
  • Save prophile/646276 to your computer and use it in GitHub Desktop.
Save prophile/646276 to your computer and use it in GitHub Desktop.
public boolean hassum(int target) {
return hassum_recursive(-1, 0, 0, target);
}
private boolean hassum_recursive(int hi, int lo, int sum, int target) {
if (sum == target) {
return true;
} else if (sum > target && lo < hi) {
return hassum_recursive(hi, lo + 1, sum - array[lo], target);
} else {
int next = hi + 1;
return (next < array.length) &&
hassum_recursive(next, lo, sum + array[next], target);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment