forked from mirandaio/codingbat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitArray.java
More file actions
23 lines (19 loc) · 859 Bytes
/
splitArray.java
File metadata and controls
23 lines (19 loc) · 859 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 sums of the two groups are the same. 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 splitArray(). (No loops needed.)
*/
public boolean splitArray(int[] nums) {
return splitArrayHelper(0, nums, 0, 0);
}
public boolean splitArrayHelper(int start, int[] nums, int group1,
int group2) {
if(start >= nums.length)
return group1 == group2;
if(splitArrayHelper(start+1, nums, group1 + nums[start], group2))
return true;
if(splitArrayHelper(start+1, nums, group1, group2 + nums[start]))
return true;
return false;
}