We’re asked to return the maximum number of unique, postive, even integers we can split from a given integer input.
Solving this greedily, we’ll only handle integer inputs that are even. We’ll start from the smallest event integer, 2, and increment by 2 when a successful split is found. We’ll keep track of the integers used to split in an array, and subtract from the input integer during each iteration.
The solution is as follows:
class Solution:
def maximumEvenSplit(self, finalSum: int) -> List[int]:
ans, i = [], 2
if not finalSum % 2:
while not i > finalSum:
ans += [i]
finalSum -= i
i += 2
ans[-1] += finalSum
return ans
_ Time Complexity:
O(sqrt(n)) - Where n is the target, we iterate sqrt(n) times.
_ Space Complexity:
O(n) - We use an array to keep track of the answer.