Formatted question description: https://leetcode.ca/all/2160.html

# 2160. Minimum Sum of Four Digit Number After Splitting Digits (Easy)

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 = 2932Output:52Explanation: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:**

Input:num = 4009Output:13Explanation: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`

**Similar Questions**:

## Solution 1. Sorting + Greedy

Get the 4 digits from `n`

. Assume they are `a <= b <= c <= d`

, the sum is minimum when `new1 = ac`

and `new2 = bd`

.

So, the answer is `10 * (a + b) + (c + d)`

.

```
// OJ: https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/
// Time: O(1) or more specifically, O(KlogK), where `K` is the number of digits in `n`
// Space: O(1) or O(K)
class Solution {
public:
int minimumSum(int n) {
int d[4] = {}, ans = 0;
for (int i = 0; i < 4; ++i, n /= 10) d[i] = n % 10;
sort(begin(d), end(d));
return 10 * (d[0] + d[1]) + d[2] + d[3];
}
};
```

## Discuss

https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/discuss/1747007/