Skip to content

Instantly share code, notes, and snippets.

@sbsatter
Created August 19, 2016 16:16
Show Gist options
  • Save sbsatter/4178264f9d9ec477014dc93e995aa48a to your computer and use it in GitHub Desktop.
Save sbsatter/4178264f9d9ec477014dc93e995aa48a to your computer and use it in GitHub Desktop.
Classic backtracking problem: Recursion-2 > groupSum prev | next | chance Given an array of ints, is it possible to choose a group of some of the ints, such that the group sums to the given target? This is a classic backtracking recursion problem. Once you understand the recursive backtracking strategy in this problem, you can use the same patte…
public boolean groupSum(int start, int[] nums, int target) {
if(target==0){
return true;
}
if(start>=nums.length){
return false;
}
if(groupSum(start+1, nums, target-nums[start])){
return true;
}else{
return groupSum(start+1, nums, target);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment