# Leetcode - Minimum Sum of Four Digit Number After Splitting Digits

Yuni·2023년 8월 8일
0

목록 보기
14/27

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


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

## Approach

As the title of the problem, I splitted digits and then took first+third digits and second+fourth digts with using an array.

1. Since num is number type, I changed it to string type, splitted them and sorted in ascending order.
2. Set array[0], array[2] as variable number type a, array[1], array[3] as variable number type b
3. return a+b

### code

/**
* @param {number} num
* @return {number}
*/
var minimumSum = function(num) {
let arr = num.toString().split('').sort();
const a = parseInt(arr[0]+arr[2]);
const b = parseInt(arr[1]+arr[3]);
return a+b;
};
Look at art, make art, show art and be art. So does as code.