Skip to content

Commit 4e7fa41

Browse files
author
Alfredo Miranda
committed
Added groupSum5.java
1 parent 1f31f12 commit 4e7fa41

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

java/recursion-2/groupSum5.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* Given an array of ints, is it possible to choose a group of some of the
2+
* ints, such that the group sums to the given target with these additional
3+
* constraints: all multiples of 5 in the array must be included in the group.
4+
* If the value immediately following a multiple of 5 is 1, it must not be
5+
* chosen. (No loops needed.)
6+
*/
7+
public boolean groupSum5(int start, int[] nums, int target) {
8+
if(start >= nums.length)
9+
return target == 0;
10+
11+
if(nums[start] % 5 == 0) {
12+
if(start <= nums.length - 2 && nums[start+1] == 1)
13+
return groupSum5(start+2, nums, target - nums[start]);
14+
15+
return groupSum5(start+1, nums, target - nums[start]);
16+
}
17+
18+
if(groupSum5(start+1, nums, target - nums[start]))
19+
return true;
20+
21+
if(groupSum5(start+1, nums, target))
22+
return true;
23+
24+
return false;
25+
}

0 commit comments

Comments
 (0)