2160. Minimum Sum of Four Digit Number After Splitting Digits LeetCode Solution

In this guide, you will get 2160. Minimum Sum of Four Digit Number After Splitting Digits LeetCode Solution with the best time and space complexity. The solution to Minimum Sum of Four Digit Number After Splitting Digits 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. Minimum Sum of Four Digit Number After Splitting Digits solution in C++
  4. Minimum Sum of Four Digit Number After Splitting Digits solution in Java
  5. Minimum Sum of Four Digit Number After Splitting Digits solution in Python
  6. Additional Resources
2160. Minimum Sum of Four Digit Number After Splitting Digits LeetCode Solution image

Problem Statement of Minimum Sum of Four Digit Number After Splitting Digits

You are given a positive integer num consisting of exactly four digits. Split num into two new integers new1 and new2 by using the digits found in num. Leading zeros are allowed in new1 and new2, and all the digits found in num must be used.

For example, given num = 2932, you have the following digits: two 2’s, one 9 and one 3. Some of the possible pairs [new1, new2] are [22, 93], [23, 92], [223, 9] and [2, 329].

Return the minimum possible sum of new1 and new2.

Example 1:

Input: num = 2932
Output: 52
Explanation: Some possible pairs [new1, new2] are [29, 23], [223, 9], etc.
The minimum sum can be obtained by the pair [29, 23]: 29 + 23 = 52.

See also  1497. Check If Array Pairs Are Divisible by k LeetCode Solution

Example 2:

Input: num = 4009
Output: 13
Explanation: Some possible pairs [new1, new2] are [0, 49], [490, 0], etc.
The minimum sum can be obtained by the pair [4, 9]: 4 + 9 = 13.

Constraints:

1000 <= num <= 9999

Complexity Analysis

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

2160. Minimum Sum of Four Digit Number After Splitting Digits LeetCode Solution in C++

class Solution {
 public:
  int minimumSum(int num) {
    string s = to_string(num);
    ranges::sort(s);
    return stoi(s.substr(0, 1) + s.substr(2, 1)) +
           stoi(s.substr(1, 1) + s.substr(3, 1));
  }
};
/* code provided by PROGIEZ */

2160. Minimum Sum of Four Digit Number After Splitting Digits LeetCode Solution in Java

class Solution {
  public int minimumSum(int num) {
    char[] chars = String.valueOf(num).toCharArray();
    Arrays.sort(chars);
    return (chars[0] - '0') * 10 + (chars[2] - '0') + (chars[1] - '0') * 10 + (chars[3] - '0');
  }
}
// code provided by PROGIEZ

2160. Minimum Sum of Four Digit Number After Splitting Digits LeetCode Solution in Python

class Solution:
  def minimumSum(self, num: int) -> int:
    s = sorted(str(num))
    return int(s[0] + s[2]) + int(s[1] + s[3])
# code by PROGIEZ

Additional Resources

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