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 Integers12Input: finalSumSplit into maxunique evensGreedy Strategy1. Start with smallest: 2, 4, 6...2. Continue while sum ≤ target2462 + 4 + 6 = 12 ✓Output: [2, 4, 6]Maximum count: 3 unique even integersOther Valid Splits:[12] → count = 1[2, 10] → count = 2[4, 8] → count = 2But [2,4,6] has max count!
Understanding the Visualization
1
Input
Given finalSum = 12, need unique positive even integers
2
Process
Use greedy approach: start with smallest evens 2,4,6...
3
Output
Return [2,4,6] with maximum count of 3
Key Takeaway
🎯 Key Insight: Start with the smallest even numbers to maximize the count, using greedy approach for optimal O(√n) solution
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