Given an array of ints, is it possible to divide the ints into two groups, so that the sum of one group is a multiple of 10, and the sum of the other group is odd. Every int must be in one group or the other. Write a recursive helper method that takes whatever arguments you like, and make the initial call to your recursive helper from splitOdd10(). (No loops needed.)
splitOdd10({5, 5, 5}) → true splitOdd10({5, 5, 6}) → false splitOdd10({5, 5, 6, 1}) → true
public boolean splitOdd10(int[] nums) {
int total=findSum(0, nums, 0);
if((total%10)%2==0||total==0)
{
return false;
}
return ifGroupExistsThatSumsToValue(0, nums, total%10);
}
public int findSum(int n, int[] nums, int sum)
{
if(nums.length==0)
return 0;
if(n==nums.length-1)
{
return sum+nums[n];
}
return findSum(n+1, nums, sum+nums[n]);
}
public boolean ifGroupExistsThatSumsToValue(int start, int[]nums, int target)
{
if(start==nums.length-1)
{
return target-nums[start]==0;
}
if(target==0)
{
return true;
}
if(ifGroupExistsThatSumsToValue(start+1, nums, target-nums[start]))
return ifGroupExistsThatSumsToValue(start+1, nums, target-nums[start]);
return ifGroupExistsThatSumsToValue(start+1, nums, target+nums[start]);
}
(total % 10) % 2==0
is exactly the same astotal % 2==0
. \$\endgroup\$