# LeetCode 2160. 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.

## My Solution

My approach was to split the number into an array of digits and sort the digits in ascending order. At this point you just need to combine either of the left 2 digits with either of the right 2 digits in order to get the correct answer. It only matters that the 2 smallest digits are in the tens place and the two largest numbers are in the ones place. So could be (0,2) + (1,3) or (0,3) + (1,2).

I converted the number to a string and left it as a string for sorting. Reason behind this is that strings are easy to concatenate in the final line to get the 2 digit numbers.

I should start keeping a timer. I solved this one quickly, in 2-3 minutes.

```
/**
* @param {number} num
* @return {number}
*/
var minimumSum = function(num) {
let array = String(num).split('');
let sortedarray = array.sort();
return Number(sortedarray[0] + sortedarray[3]) + Number(sortedarray[1] + sortedarray[2]);
};
```

## A Nice Solution

This solution is almost the same as mine.

Some notes on what I learned.

- Array sort() method sorts in place. I didn’t need that extra assignment. Although it doesn’t really matter because the original array is returned. No copy is made.
- This syntax array.sort((a, b) => a - b). Take taking me some time to wrap my head around it. I’ve seen a lot of arrow functions in C# so it makes sense to me. a and b are both strings. There is an arrow compare function. If a - b is negative it means a comes before b in the character set, the element a should come before element b in the array. If a - b is positive it means a is bigger than b, comes after b in the character set. b should come before a in the array. I should add that the elements of array are already strings, and array sort method compares as strings by default so this compare function is redundant.

```
/**
* @param {number} num
* @return {number}
*/
var minimumSum = function(num) {
const array = num.toString().split('')
array.sort((a, b) => a - b)
return Number(array[0] + array[2]) + Number(array[1] + array[3])
};
```