For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.
Finally we’ve reached the last section of Coding Bat: Recursion-2. Those nine exercises are all very similar. Once you’ve figured out one you can probably solve the others immediately.
I’ll just walk you through the general strategy, using the first exercise as an example: The method “groupSum” takes three arguments. The first, “start”, is the index of an array of integers, the second is the array of integers in question, and the third, “target” is the target value. Now, “start” increases by one with each recursive call. If this value is greater than or equal to the length of the array, then the entire array has been processed. At this point, only the evaluation step has to be done, which consists of checking whether the target value is zero.
“Why zero?”, you may ask. If “target” equals zero, then it is possible to pick a group of integers from the array that sum up to the target value. The calculation is done by selecting each value of the array and in one case subtracting it from “target”, while in the other leaving “target” unchanged. Thus, all possible combinations will eventually be checked. Finally, the method returns “true” once one of the results is “true”, which is a property of short-circuit evaluation of logical disjunctions.
All solutions were successfully tested on 28 March 2013.
groupSum:
public boolean groupSum(int start, int[] nums, int target) {
if (start >= nums.length) return target == 0;
return groupSum(start + 1, nums, target - nums[start])
|| groupSum(start + 1, nums, target);
}
groupSum6:
public boolean groupSum6(int start, int[] nums, int target) {
if (start >= nums.length) return target == 0;
if (nums[start] == 6)
return groupSum6(start + 1, nums, target - nums[start]);
return groupSum6(start + 1, nums, target - nums[start])
|| groupSum6(start + 1, nums, target);
}
groupNoAdj:
public boolean groupNoAdj(int start, int[] nums, int target) {
if (start >= nums.length) return target == 0;
return groupNoAdj(start + 2, nums, target - nums[start])
|| groupNoAdj(start + 1, nums, target);
}
groupSum5:
public boolean groupSum5(int start, int[] nums, int target) {
if (start >= nums.length) return target == 0;
if (nums[start] % 5 == 0) {
if (start < nums.length - 1 && nums[start + 1] == 1)
return groupSum5(start + 2, nums, target - nums[start]);
return groupSum5(start + 1, nums, target - nums[start]);
}
return groupSum5(start + 1, nums, target - nums[start])
|| groupSum5(start + 1, nums, target);
}
[/sourcecode]
<b>groupSumClump:</b>
[sourcecode language="Java" gutter="true"]
public boolean groupSumClump(int start, int[] nums, int target) {
if (start >= nums.length) return target == 0;
int sum = nums[start];
int count = 1;
for (int i = start + 1; i < nums.length; i++)
if (nums[i] == nums[start]) {
sum += nums[i];
count++;
}
return groupSumClump(start + count, nums, target - sum)
|| groupSumClump(start + count, nums, target);
}
[/sourcecode]
<b>splitArray:</b>
[sourcecode language="Java" gutter="true"]
public boolean splitArray(int[] nums) {
return helper(0, nums, 0, 0);
}
private boolean helper(int start, int[] nums, int sum1, int sum2) {
if (start >= nums.length) return sum1 == sum2;
return helper(start + 1, nums, sum1 + nums[start], sum2)
|| helper(start + 1, nums, sum1, sum2 + nums[start]);
}
splitOdd10:
public boolean splitOdd10(int[] nums) {
return helper(0, nums, 0, 0);
}
private boolean helper(int start, int[] nums, int sum1, int sum2) {
if (start >= nums.length)
return sum1 % 10 == 0 && sum2 % 2 == 1 || sum1 % 2 == 1
&& sum2 % 10 == 0;
return helper(start + 1, nums, sum1 + nums[start], sum2)
|| helper(start + 1, nums, sum1, sum2 + nums[start]);
}
split53:
public boolean split53(int[] nums) {
return helper(0, nums, 0, 0);
}
private boolean helper(int start, int[] nums, int sum1, int sum2) {
if (start >= nums.length) return sum1 == sum2;
if (nums[start] % 5 == 0)
return helper(start + 1, nums, sum1 + nums[start], sum2);
if (nums[start] % 3 == 0)
return helper(start + 1, nums, sum1, sum2 + nums[start]);
return helper(start + 1, nums, sum1 + nums[start], sum2)
|| helper(start + 1, nums, sum1, sum2 + nums[start]);
}
For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.