2178. Maximum Split of Positive Even Integers LeetCode Solution

In this guide, you will get 2178. Maximum Split of Positive Even Integers LeetCode Solution with the best time and space complexity. The solution to Maximum Split of Positive Even Integers problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Split of Positive Even Integers solution in C++
  4. Maximum Split of Positive Even Integers solution in Java
  5. Maximum Split of Positive Even Integers solution in Python
  6. Additional Resources
2178. Maximum Split of Positive Even Integers LeetCode Solution image

Problem Statement of 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 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.

See also  1722. Minimize Hamming Distance After Swap Operations LeetCode Solution

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 <= 1010

Complexity Analysis

  • Time Complexity: O(\sqrt{\texttt{finalSum}})
  • Space Complexity: O(\sqrt{\texttt{finalSum}})

2178. Maximum Split of Positive Even Integers LeetCode Solution in C++

class Solution {
 public:
  vector<long long> maximumEvenSplit(long long finalSum) {
    if (finalSum % 2 == 1)
      return {};

    vector<long long> ans;
    long needSum = finalSum;
    long even = 2;

    while (needSum - even >= even + 2) {
      ans.push_back(even);
      needSum -= even;
      even += 2;
    }

    ans.push_back(needSum);
    return ans;
  }
};
/* code provided by PROGIEZ */

2178. Maximum Split of Positive Even Integers LeetCode Solution in Java

class Solution {
  public List<Long> maximumEvenSplit(long finalSum) {
    if (finalSum % 2 == 1)
      return new ArrayList<>();

    List<Long> ans = new ArrayList<>();
    long needSum = finalSum;
    long even = 2;

    while (needSum - even >= even + 2) {
      ans.add(even);
      needSum -= even;
      even += 2;
    }

    ans.add(needSum);
    return ans;
  }
}
// code provided by PROGIEZ

2178. Maximum Split of Positive Even Integers LeetCode Solution in Python

class Solution:
  def maximumEvenSplit(self, finalSum: int) -> list[int]:
    if finalSum % 2 == 1:
      return []

    ans = []
    needSum = finalSum
    even = 2

    while needSum - even >= even + 2:
      ans.append(even)
      needSum -= even
      even += 2

    return ans + [needSum]
# code by PROGIEZ

Additional Resources

See also  780. Reaching Points LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.