Skip to content

Commit c835c44

Browse files
author
Alfredo Miranda
committed
Added split53.java
1 parent 3867a8e commit c835c44

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

java/recursion-2/split53.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* Given an array of ints, is it possible to divide the ints into two groups,
2+
* so that the sum of the two groups is the same, with these constraints: all
3+
* the values that are multiple of 5 must be in one group, and all the values
4+
* that are a multiple of 3 (and not a multiple of 5) must be in the other.
5+
* (No loops needed.)
6+
*/
7+
public boolean split53(int[] nums) {
8+
return split53Helper(0, nums, 0, 0);
9+
}
10+
11+
public boolean split53Helper(int start, int[] nums, int mult5, int mult3) {
12+
if(start >= nums.length)
13+
return mult5 == mult3;
14+
15+
if(nums[start] % 5 == 0)
16+
return split53Helper(start+1, nums, mult5 + nums[start], mult3);
17+
18+
if(nums[start] % 3 == 0)
19+
return split53Helper(start+1, nums, mult5, mult3 + nums[start]);
20+
21+
if(split53Helper(start+1, nums, mult5 + nums[start], mult3))
22+
return true;
23+
24+
if(split53Helper(start+1, nums, mult5, mult3 + nums[start]))
25+
return true;
26+
27+
return false;
28+
}

0 commit comments

Comments
 (0)