Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

split53 challenge

Hi,

 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
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@gudii9
You didn't forget this question, do you?
Avatar of gudii9

ASKER

split53([1, 1]) → true

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?
Avatar of gudii9

ASKER

i am still trying to understand backward recursion concept
Avatar of gudii9

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]);
}

Open in new window


in above solution why we are using helper method instead of regular call of recursion?
You could do it without that helper method too.