2178. Maximum Split of Positive Even Integers
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 tofinalSum
):(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 . 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 . Thus, we return [6,8,2,12]
.
Note that [10,2,4,12]
, [6,2,4,16]
, etc. are also accepted.
Constraints:
-
finalSum
Solution
Full Solution
First, let's think of how to determine if a sum can have a valid split that contains exactly integers.
The first case we should consider is whether or not a sum can have a valid split of any size. Since our split includes only even integers, will only have a split if it's even and we will return an empty list if is odd.
One observation we can make is that if some sum does have a valid split of integers, then a sum will also have a valid split of integers if is even and . Why is this true? Let's denote as the difference between and . From the split of integers from the sum , incrementing the largest integer in the split by results in a valid split of integers with a total sum of . It can be observed that increasing the greatest integer will always keep the entire list distinct.
Example
For this example, let's use the split with and . How will we construct a split of size with sum ?
First, we'll find the difference . Then, we'll add to the greatest integer in the split with sum , which is .
Thus, we obtain the split with sum and size .
Back to the Original Problem
We are given and asked to find the maximum possible and construct a split of size .
If we take the sum of the smallest positive even integers (2 + 4 + 6 + 8 + ... + 2*K)
, we'll obtain the least possible sum that has a split of size . Let's denote this sum as low
. A sum will have a valid split of size if low
.
To solve the problem, we'll first find the maximum where low
. Starting with the split that sums to low
, we'll add low
to the largest integer to obtain our final split for .
Simulation
Time Complexity
For a sum with a split of maximum size , low = 2 + 4 + 6 + 8 + ... + 2*k
. In the sum, there are elements and the average element is , resulting in and . Since our algorithm runs in , our final time complexity is .
Time Complexity:
Space Complexity
Since we construct a list of size , our space complexity is also .
Space Complexity:
C++ Solution
class Solution {
public:
vector<long long> maximumEvenSplit(long long finalSum) {
vector<long long> ans; // integers in our split
if (finalSum % 2 == 1) { // odd sum provides no solution
return ans;
}
long long currentSum = 0; // keep track of the value of low
int i = 1;
while (currentSum + 2 * i <=
finalSum) { // keep increasing size of split until maximum
currentSum += 2 * i;
ans.push_back(2 * i);
i++;
}
ans[ans.size() - 1] +=
finalSum - currentSum; // add S - low to largest element
return ans;
}
};
Java Solution
class Solution {
public List<Long> maximumEvenSplit(long finalSum) {
List<Long> ans = new ArrayList<Long>(); // integers in our split
if (finalSum % 2 == 1) { // odd sum provides no solution
return ans;
}
long currentSum = 0; // keep track of the value of low
int i = 1;
while (currentSum + 2 * i
<= finalSum) { // keep increasing size of split until maximum
currentSum += 2 * i;
ans.add((long) 2 * i);
i++;
}
int idx = ans.size() - 1;
ans.set(idx,
ans.get(idx) + finalSum
- currentSum); // add S - low to largest element
return ans;
}
}
Python Solution
class Solution: def maximumEvenSplit(self, finalSum: int) -> List[int]: ans = [] # integers in our split if finalSum % 2 == 1: # odd sum provides no solution return ans currentSum = 0 i = 1 while ( currentSum + 2 * i <= finalSum ): # keep increasing size of split until maximum currentSum += 2 * i ans.append(2 * i) i += 1 ans[len(ans) - 1] += finalSum - currentSum # add S - low to largest element return ans
Javascript Solution
/** * @param {number} finalSum * @return {number[]} */ var maximumEvenSplit = function (finalSum) { let ans = []; // integers in our split if (finalSum % 2 === 1) { // odd sum provides no solution return ans; } let currentSum = 0; // keep track of the value of low let i = 1; while (currentSum + 2 * i <= finalSum) { // keep increasing size of split until maximum currentSum += 2 * i; ans.push(2 * i); i++; } ans[ans.length - 1] += finalSum - currentSum; // add S - low to largest element return ans; };
Ready to land your dream job?
Unlock your dream job with a 2-minute evaluator for a personalized learning plan!
Start EvaluatorWhich of the following shows the order of node visit in a Breadth-first Search?
Recommended Readings
LeetCode Patterns Your Personal Dijkstra's Algorithm to Landing Your Dream Job The goal of AlgoMonster is to help you get a job in the shortest amount of time possible in a data driven way We compiled datasets of tech interview problems and broke them down by patterns This way we
Recursion Recursion is one of the most important concepts in computer science Simply speaking recursion is the process of a function calling itself Using a real life analogy imagine a scenario where you invite your friends to lunch https algomonster s3 us east 2 amazonaws com recursion jpg You first
Runtime Overview When learning about algorithms and data structures you'll frequently encounter the term time complexity This concept is fundamental in computer science and offers insights into how long an algorithm takes to complete given a certain input size What is Time Complexity Time complexity represents the amount of time
Want a Structured Path to Master System Design Too? Don’t Miss This!