gudii9
asked on
split53 challenge
Hi,
I am working on below challenge
http://codingbat.com/prob/p168295
I was not clear how above are true and false and true.
split53([1, 1]) → true //here I do not see any multiple of 5 or 3??
same with below also
split53([1, 1, 1]) → false
split53([2, 4, 2])
please advise
I am working on below challenge
http://codingbat.com/prob/p168295
[/Given an array of ints, is it possible to divide the ints into two groups, so that the sum of the two groups is the same, with these constraints: all the values that are multiple of 5 must be in one group, and all the values that are a multiple of 3 (and not a multiple of 5) must be in the other. (No loops needed.)
split53([1, 1]) → true
split53([1, 1, 1]) → false
split53([2, 4, 2]) → true
I was not clear how above are true and false and true.
split53([1, 1]) → true //here I do not see any multiple of 5 or 3??
same with below also
split53([1, 1, 1]) → false
split53([2, 4, 2])
please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
split53([1, 1]) → true
how above is true
1 is neither multiple of 5 nor 3?
how above is true
1 is neither multiple of 5 nor 3?
The 'true' is an answer to the question:
Is it possible to divide the ints into two groups, so that the sum of the two groups is the same?
Is it possible to divide the ints into two groups, so that the sum of the two groups is the same?
ASKER
i am still trying to understand backward recursion concept
ASKER
ublic 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 [b]helper[/b](start + 1, nums, sum1 + nums[start], sum2)
|| [b]helper[/b](start + 1, nums, sum1, sum2 + nums[start]);
}
in above solution why we are using helper method instead of regular call of recursion?
You could do it without that helper method too.
You didn't forget this question, do you?