1
+ package solutions ;
2
+
3
+ import java .util .*;
4
+
5
+ // [Problem] https://leetcode.com/problems/maximum-split-of-positive-even-integers
6
+ class MaximumSplitOfIntegers {
7
+ // Min heap
8
+ // O(nlogn) time, O(n) space
9
+ // where n = total number of meetings in schedule
10
+ public List <Long > maximumEvenSplit (long finalSum ) {
11
+ if (finalSum % 2 == 1 ) {
12
+ return new ArrayList <>();
13
+ }
14
+ List <Long > numbers = new ArrayList <>();
15
+ long currentNumber = 2L ;
16
+ while (finalSum > 0 ) {
17
+ if (finalSum - currentNumber > currentNumber ) {
18
+ finalSum -= currentNumber ;
19
+ numbers .add (currentNumber );
20
+ } else {
21
+ numbers .add (finalSum );
22
+ break ;
23
+ }
24
+ currentNumber += 2 ;
25
+ }
26
+ return numbers ;
27
+ }
28
+
29
+ // Test
30
+ public static void main (String [] args ) {
31
+ MaximumSplitOfIntegers solution = new MaximumSplitOfIntegers ();
32
+
33
+ long input1 = 12 ;
34
+ List <Long > expectedOutput1 = List .of (2L , 4L , 6L );
35
+ List <Long > actualOutput1 = solution .maximumEvenSplit (input1 );
36
+ System .out .println ("Test 1 passed? " + expectedOutput1 .equals (actualOutput1 ));
37
+
38
+ long input2 = 28 ;
39
+ List <Long > expectedOutput2 = List .of (2L , 4L , 6L , 16L );
40
+ List <Long > actualOutput2 = solution .maximumEvenSplit (input2 );
41
+ System .out .println ("Test 2 passed? " + expectedOutput2 .equals (actualOutput2 ));
42
+ }
43
+ }
0 commit comments