forked from mirandaio/codingbat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitOdd10.java
More file actions
23 lines (19 loc) · 897 Bytes
/
splitOdd10.java
File metadata and controls
23 lines (19 loc) · 897 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* 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.)
*/
public boolean splitOdd10(int[] nums) {
return splitOdd10Helper(0, nums, 0, 0);
}
public boolean splitOdd10Helper(int start, int[] nums, int mult, int odd) {
if(start >= nums.length)
return mult % 10 == 0 && odd % 2 == 1;
if(splitOdd10Helper(start+1, nums, mult + nums[start], odd))
return true;
if(splitOdd10Helper(start+1, nums, mult, odd + nums[start]))
return true;
return false;
}