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