Problem

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 _theminimum possible sum of _new1 andnew2.

Examples

Example 1

1
2
3
4
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.

Example 2

1
2
3
4
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

Solution

Method 1 – Greedy Digit Assignment

Intuition

To minimize the sum, sort the digits and assign the smallest two digits to the tens place of each number, and the largest two to the ones place. This ensures the two numbers formed are as small as possible.

Approach

  1. Extract the digits of num and sort them.
  2. Form two numbers: new1 = digits[0]*10 + digits[2], new2 = digits[1]*10 + digits[3].
  3. Return new1 + new2.

Code

1
2
3
4
5
6
7
8
9
class Solution {
public:
    int minimumSum(int num) {
        vector<int> d(4);
        for (int i = 0; i < 4; ++i) { d[i] = num % 10; num /= 10; }
        sort(d.begin(), d.end());
        return d[0]*10 + d[1]*10 + d[2] + d[3];
    }
};
1
2
3
4
5
6
func minimumSum(num int) int {
    d := make([]int, 4)
    for i := 0; i < 4; i++ { d[i] = num % 10; num /= 10 }
    sort.Ints(d)
    return d[0]*10 + d[1]*10 + d[2] + d[3]
}
1
2
3
4
5
6
7
8
class Solution {
    public int minimumSum(int num) {
        int[] d = new int[4];
        for (int i = 0; i < 4; i++) { d[i] = num % 10; num /= 10; }
        Arrays.sort(d);
        return d[0]*10 + d[1]*10 + d[2] + d[3];
    }
}
1
2
3
4
5
6
7
8
9
class Solution {
    fun minimumSum(num: Int): Int {
        val d = IntArray(4)
        var n = num
        for (i in 0..3) { d[i] = n % 10; n /= 10 }
        d.sort()
        return d[0]*10 + d[1]*10 + d[2] + d[3]
    }
}
1
2
3
4
class Solution:
    def minimumSum(self, num: int) -> int:
        d = sorted([int(x) for x in str(num)])
        return d[0]*10 + d[1]*10 + d[2] + d[3]
1
2
3
4
5
6
7
8
9
impl Solution {
    pub fn minimum_sum(num: i32) -> i32 {
        let mut d = vec![];
        let mut n = num;
        for _ in 0..4 { d.push(n%10); n/=10; }
        d.sort();
        d[0]*10 + d[1]*10 + d[2] + d[3]
    }
}
1
2
3
4
5
6
class Solution {
    minimumSum(num: number): number {
        const d = String(num).split('').map(Number).sort((a,b)=>a-b);
        return d[0]*10 + d[1]*10 + d[2] + d[3];
    }
}

Complexity

  • ⏰ Time complexity: O(1) — Only 4 digits to sort and combine.
  • 🧺 Space complexity: O(1) — Only 4 digits stored.