Skip to main content
Back to problems
#2160
Easy Algorithms

Minimum sum of four digit number after splitting digits

Math Greedy Sorting
86.2% acceptance
Feb 25, 2026
1505
148
You are given a positive integer num consisting of exactly four digits. Split num into two new integers new1 and new2 using the digits found in num (all digits must be used). Return the minimum possible sum of new1 and new2.

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn minimum_sum(num: i32) -> i32 {
    let mut digits = [
      num / 1000,
      (num / 100) % 10,
      (num / 10) % 10,
      num % 10,
    ];
    digits.sort_unstable();
    // Assign smallest two digits as tens digits, largest two as units digits
    // new1 = d[0]*10 + d[2], new2 = d[1]*10 + d[3]
    // Sum = 10*(d[0]+d[1]) + (d[2]+d[3])
    digits[0] * 10 + digits[1] * 10 + digits[2] + digits[3]
  }
}