Skip to main content

Leetcode - 2160. Minimum Sum of Four Digit Number After Splitting Digits

· One min read

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

Solution 1

/**
* @param {number} num
* @return {number}
*/
var minimumSum = function (num) {
const numToArr = num.toString().split('');

for (let i = 1; i < numToArr.length; i++) {
for (let j = 0; j < i; j++) {
if (numToArr[i] < numToArr[j]) {
const smallNum = numToArr[i];
numToArr[i] = numToArr[j];
numToArr[j] = smallNum;
}
}
}

const new1 = numToArr[0] + numToArr[2];
const new2 = numToArr[1] + numToArr[3];

return Number(new1) + Number(new2);
};