2578. Split With Minimum Sum LeetCode Solution

In this guide, you will get 2578. Split With Minimum Sum LeetCode Solution with the best time and space complexity. The solution to Split With Minimum Sum 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. Split With Minimum Sum solution in C++
  4. Split With Minimum Sum solution in Java
  5. Split With Minimum Sum solution in Python
  6. Additional Resources
2578. Split With Minimum Sum LeetCode Solution image

Problem Statement of Split With Minimum Sum

Given a positive integer num, split it into two non-negative integers num1 and num2 such that:

The concatenation of num1 and num2 is a permutation of num.

In other words, the sum of the number of occurrences of each digit in num1 and num2 is equal to the number of occurrences of that digit in num.

num1 and num2 can contain leading zeros.

Return the minimum possible sum of num1 and num2.
Notes:

It is guaranteed that num does not contain any leading zeros.
The order of occurrence of the digits in num1 and num2 may differ from the order of occurrence of num.

Example 1:

Input: num = 4325
Output: 59
Explanation: We can split 4325 so that num1 is 24 and num2 is 35, giving a sum of 59. We can prove that 59 is indeed the minimal possible sum.

Example 2:

Input: num = 687
Output: 75
Explanation: We can split 687 so that num1 is 68 and num2 is 7, which would give an optimal sum of 75.

See also  2924. Find Champion II LeetCode Solution

Constraints:

10 <= num <= 109

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

2578. Split With Minimum Sum LeetCode Solution in C++

class Solution {
 public:
  int splitNum(int num) {
    string num1;
    string num2;
    string s = to_string(num);

    ranges::sort(s);

    for (int i = 0; i < s.length(); ++i)
      if (i % 2 == 0)
        num1 += s[i];
      else
        num2 += s[i];

    return stoi(num1) + stoi(num2);
  }
};
/* code provided by PROGIEZ */

2578. Split With Minimum Sum LeetCode Solution in Java

class Solution {
  public int splitNum(int num) {
    StringBuilder sb1 = new StringBuilder();
    StringBuilder sb2 = new StringBuilder();
    char[] chars = String.valueOf(num).toCharArray();

    Arrays.sort(chars);

    for (int i = 0; i < chars.length; ++i)
      if (i % 2 == 0)
        sb1.append(chars[i]);
      else
        sb2.append(chars[i]);

    return Integer.parseInt(sb1.toString()) + Integer.parseInt(sb2.toString());
  }
}
// code provided by PROGIEZ

2578. Split With Minimum Sum LeetCode Solution in Python

class Solution:
  def splitNum(self, num: int) -> int:
    s = ''.join(sorted(str(num)))
    return sum(map(int, (s[::2], s[1::2])))
# code by PROGIEZ

Additional Resources

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