This is the problem I'm working on: "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 with these additional constraints: all multiples of 5 in the array must be included in the group. If the value immediately following a multiple of 5 is 1, it must not be chosen. (No loops needed.)"
I tried the following:
public boolean groupSum5(int start, int[] nums, int target) {
if (start == nums.length) return (target == 0);
if (groupSum5(start + 1, nums, target - nums[start]) && nums[start] % 5 == 0)
return true;
if (groupSum5(start + 1, nums, target)) return true;
return false;
}
But it only gets the multiples of 5, and I tried this:
public boolean groupSum5(int start, int[] nums, int target) {
if (start == nums.length) return (target == 0);
if (groupSum5(start + 1, nums, target - nums[start]) && nums[start] % 5 == 0)
return true;
if (groupSum(start + 1, nums, target - nums[start])) return true;
if (groupSum5(start + 1, nums, target)) return true;
return false;
}
but it does not work, since sometimes the multiples of 5 are not included.
I know my code does not fullfil the second constraint, yet.
Any ideas?
