Maximum Split of Positive Even Integers - Problem

You are given an integer finalSum. Split it into a sum of a maximum number of unique positive even integers.

For example, given 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.

Input & Output

Example 1 — Basic Case
$ Input: finalSum = 12
Output: [2,4,6]
💡 Note: We can split 12 into 2+4+6=12. This gives us 3 unique even integers, which is maximum possible. Other valid splits like (12) or (2,10) have fewer integers.
Example 2 — Small Sum
$ Input: finalSum = 7
Output: []
💡 Note: Since 7 is odd, it cannot be expressed as a sum of even integers. Return empty array.
Example 3 — Single Number
$ Input: finalSum = 28
Output: [2,4,6,16]
💡 Note: Start with 2,4,6,8,10... but 2+4+6+8+10=30>28. So we use [2,4,6] and remaining 28-12=16, giving [2,4,6,16].

Constraints

  • 1 ≤ finalSum ≤ 1010

Visualization

Tap to expand
Maximum Split of Positive Even Integers INPUT finalSum 12 Visualizing value 12: 12 units total Goal: Split into MAXIMUM unique positive even integers Constraints: - All numbers must be EVEN - All numbers must be UNIQUE - Sum must equal 12 ALGORITHM STEPS 1 Check if odd 12 is EVEN - proceed 2 Start greedy Take smallest evens: 2,4,6... 3 Add while possible remaining >= next even 4 Adjust last number Add remainder to last Execution Trace: remain=12, take 2 --> [2] remain=10, take 4 --> [2,4] remain=6, take 6 --> [2,4,6] remain=0, DONE! 2 + 4 + 6 = 12 [OK] FINAL RESULT Visual Split of 12: 2 4 6 + + = 12 Output Array: [2, 4, 6] Verification: All EVEN: 2,4,6 [OK] All UNIQUE: yes [OK] Sum = 12 [OK] Key Insight: Greedy approach works because taking smallest even numbers first maximizes the count of integers. If we have remainder, we add it to the last number (keeping it even). Odd finalSum has no valid split since sum of even numbers is always even. Time: O(sqrt(n)), Space: O(sqrt(n)) for result array. TutorialsPoint - Maximum Split of Positive Even Integers | Greedy - Sequential Even Numbers
Asked in
Google 15 Facebook 12 Amazon 8
23.4K Views
Medium Frequency
~15 min Avg. Time
842 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen