Minimum Sum of Four Digit Number After Splitting Digits

Easy
544.

Minimum Sum of Four Digit Number After Splitting Digits

Minimum Sum of Four Digit Number After Splitting Digits is an easy coding problem on math, sorting and greedy, asked in Amazon and Adobe interviews.

You are given a four-digit integer num. Split its digits into two new integers new1 and new2, using all four digits exactly once between them.

Leading zeros are allowed, and a new integer may have one or two digits. Return the minimum possible value of new1 + new2.

Example 1

Input
num = 2932
Output
52
Explanation
Split into 29 and 23 → 52. (Digits sorted: 2,2,3,9 → 23 + 29.)

Example 2

Input
num = 4009
Output
13
Explanation
Split into 04 and 09 → 4 + 9 = 13.

Example 3

Input
num = 1111
Output
22

Constraints

  • 1000 <= num <= 9999
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number} num
 * @return {number}
 */
var minimumSum = function(num) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Minimum Sum of Four Digit Number After Splitting Digits Solution

Minimum Sum of Four Digit Number After Splitting Digits solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript.

Minimum Sum of Four Digit Number After Splitting Digits is an easy coding problem on math, sorting and greedy, asked in Amazon and Adobe interviews.

  • Difficulty: Easy
  • Topics: Math, Sorting, Greedy
  • Asked at: Amazon, Adobe
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

You are given a four-digit integer num. Split its digits into two new integers new1 and new2, using all four digits exactly once between them.

Leading zeros are allowed, and a new integer may have one or two digits. Return the minimum possible value of new1 + new2.

Example 1

Input: num = 2932
Output: 52
Explanation: Split into 29 and 23 → 52. (Digits sorted: 2,2,3,9 → 23 + 29.)

Example 2

Input: num = 4009
Output: 13
Explanation: Split into 04 and 09 → 4 + 9 = 13.

Example 3

Input: num = 1111
Output: 22

Constraints

  • 1000 <= num <= 9999

Hints for Minimum Sum of Four Digit Number After Splitting Digits

  1. Hint 1

    Both new numbers should have two digits — putting three digits in one number wastes a hundreds place.

  2. Hint 2

    The two smallest digits belong in the tens places; the two largest go in the units places.

  3. Hint 3

    Sort the four digits and pair them crosswise.

How to solve Minimum Sum of Four Digit Number After Splitting Digits

The sum is 10·(two tens digits) + (two units digits), so the tens places should hold the two smallest digits. Sorting makes the assignment obvious.

Approach

  1. Extract the four digits and sort them ascending as d0 <= d1 <= d2 <= d3.
  2. Form new1 = d0·10 + d2 and new2 = d1·10 + d3.
  3. Return their sum, which equals 10·(d0 + d1) + (d2 + d3).

Why it works

Whatever the split, the sum is ten times the digits placed in tens positions plus the digits placed in units positions. Minimising it means putting the two smallest digits in the tens positions — and any pairing of the remaining two into the units positions gives the same total.

Complexity

  • Time — O(1)
  • Space — O(1)

Pitfalls

  • Splitting as a three-digit and a one-digit number is always worse — the hundreds place multiplies a digit by 100.
  • Pairing as d0·10 + d1 and d2·10 + d3 puts a large digit in a tens place and overshoots.
  • Leading zeros are explicitly allowed, so 04 is a valid two-digit choice worth 4.

Reference solution

Python

def minimumSum(num: int) -> int:
    d = sorted(int(c) for c in str(num))
    return (d[0] * 10 + d[2]) + (d[1] * 10 + d[3])

Java

public static 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[2]) + (d[1] * 10 + d[3]);
}

C++

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[2]) + (d[1] * 10 + d[3]);
}

JavaScript

var minimumSum = function(num) {
    const d = String(num).split("").map(Number).sort(function(a, b) { return a - b; });
    return (d[0] * 10 + d[2]) + (d[1] * 10 + d[3]);
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 337 math problems · the whole catalogue

Learn the technique: Sorting Algorithms · Greedy Algorithms