File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed
Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments