You are given an integer finalSum. Split it into a sum of a maximum number of unique positive even integers.
finalSum = 12, the following splits are valid (unique positive even integers summing up to finalSum): (12), (2 + 10), (2 + 4 + 6), and (4 + 8). Among them, (2 + 4 + 6) contains the maximum number of integers. Note that finalSum cannot be split into (2 + 2 + 4 + 4) as all the numbers should be unique.Return a list of integers that represent a valid split containing a maximum number of integers. If no valid split exists for finalSum, return an empty list. You may return the integers in any order.
Example 1:
Input: finalSum = 12 Output: [2,4,6] Explanation: The following are valid splits:(12),(2 + 10),(2 + 4 + 6), and(4 + 8). (2 + 4 + 6) has the maximum number of integers, which is 3. Thus, we return [2,4,6]. Note that [2,6,4], [6,2,4], etc. are also accepted.
Example 2:
Input: finalSum = 7 Output: [] Explanation: There are no valid splits for the given finalSum. Thus, we return an empty array.
Example 3:
Input: finalSum = 28 Output: [6,8,2,12] Explanation: The following are valid splits:(2 + 26),(6 + 8 + 2 + 12), and(4 + 24).(6 + 8 + 2 + 12)has the maximum number of integers, which is 4. Thus, we return [6,8,2,12]. Note that [10,2,4,12], [6,2,4,16], etc. are also accepted.
Constraints:
1 <= finalSum <= 1010When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach for splitting even numbers involves trying all possible combinations to find the largest set that sums to the target. We explore every possible breakdown, checking if it works.
Here's how the algorithm would work step-by-step:
def maximum_split_brute_force(final_sum: int) -> list[int]:
if final_sum % 2 != 0:
return []
maximum_sets = []
def backtrack(remaining_sum: int, current_set: list[int], next_even_number: int):
# If the remaining sum is zero, the current set is valid.
if remaining_sum == 0:
maximum_sets.append(current_set[:])
return
# If the next even number exceeds the remaining sum, skip it.
if next_even_number > remaining_sum:
backtrack(remaining_sum, current_set, next_even_number + 2)
return
# Explore the option of not including the current even number.
backtrack(remaining_sum, current_set, next_even_number + 2)
# Explore the option of including the current even number.
current_set.append(next_even_number)
backtrack(remaining_sum - next_even_number, current_set, next_even_number + 2)
current_set.pop()
backtrack(final_sum, [], 2)
# Find the set with the largest number of elements.
longest_set = []
for current_set in maximum_sets:
if len(current_set) > len(longest_set):
longest_set = current_set
return longest_setThe best way to split the total amount into even numbers is to start with the smallest even numbers and keep adding them until you can't anymore. If the remaining amount is also even, you're done. If not, adjust the last two numbers to make it work.
Here's how the algorithm would work step-by-step:
def maximum_split(final_sum: int) -> list[int]:
result = []
current_even_number = 2
current_sum = 0
while current_sum + current_even_number <= final_sum:
# Add the current even number to the result list.
result.append(current_even_number)
current_sum += current_even_number
current_even_number += 2
remainder = final_sum - current_sum
if remainder > 0:
# Adjust the largest element if there's a remainder
result[-1] += remainder
return result| Case | How to Handle |
|---|---|
| FinalSum is odd | Return an empty list, as it is impossible to split an odd number into even integers. |
| FinalSum is 0 | Return an empty list, as 0 cannot be represented as a sum of positive even integers. |
| FinalSum is 2 | Return a list containing only 2, as it's the smallest possible even integer. |
| FinalSum is a large even number that might cause integer overflow if too many small even numbers are added initially | Use a long data type to avoid integer overflow during the summing process. |
| FinalSum is an even number but impossible to split to more than one even integer (FinalSum < 4) | The while loop condition (sum < finalSum) ensures the algorithm stops when it's impossible to further split. |
| When the remaining difference can be added to the last element | After the loop, add the remaining difference to the last element in the list instead of appending it to the result. |
| FinalSum equals to an extremely large number and the algorithm takes too long. | Consider and communicate the time complexity, and explore approaches to optimize if possible, or acknowledge potential performance limitations with very large numbers. |
| Insufficient memory for very large numbers of split positive even integers | Communicate the space complexity and memory limitations, especially when finalSum is extremely large leading to potentially huge lists. |