Skip to main content

Minimum Sum of Four Digit Number After Splitting Digits - Solution & Explanation

EasyMathGreedySorting15 min readAsked at: Amazon, Meta
Practice this problem

Problem Statement

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 Overview

Problem Overview: You are given a four-digit integer num. Split its digits into two new integers such that every digit is used exactly once and the sum of the two numbers is minimized. The order of digits inside each number matters, so placing smaller digits in higher place values reduces the total sum.

Approach 1: Brute Force Combination (O(1) time, O(1) space)

Extract the four digits and generate every possible way to distribute them into two two-digit numbers. For each arrangement, build numbers like 10*a + b and 10*c + d, then compute the total sum. Track the minimum across all permutations. Because there are only four digits, the total permutations are constant (4! possibilities). This approach is straightforward and demonstrates the full search space, but it does unnecessary work compared to the greedy observation.

Approach 2: Sorting and Minimal Pairing (O(d log d) time, O(1) space)

Extract the digits and sort them in ascending order using a sorting algorithm. The key greedy insight: to minimize the final sum, distribute the smallest digits across the highest place values of the two numbers. After sorting d0 ≤ d1 ≤ d2 ≤ d3, construct the numbers as (10*d0 + d2) and (10*d1 + d3). This balances the digits between the tens positions instead of stacking the smallest digits in the same number. The method follows a classic greedy strategy where locally optimal placement (smallest digits in highest weight positions) leads to the globally minimal sum.

Digit extraction itself is simple arithmetic using modulo and division, a common pattern in math-based problems. After sorting, constructing the two numbers and returning their sum is constant work.

Recommended for interviews: Sorting and minimal pairing. Interviewers expect you to notice that smaller digits should occupy the tens positions of both numbers. Brute force proves correctness but the greedy approach demonstrates stronger algorithmic intuition and cleaner implementation.

Approach 1: Sorting and Minimal Pairing

To achieve the minimum sum, sort the digits of the number. This allows us to pair smaller digits together, minimizing the individual numbers formed. By arranging the sorted digits into two numbers with minimal values, we can achieve the smallest sum possible.

For instance, for num = 2932, sorting results in [2, 2, 3, 9]. If we distribute them into [2, 3] and [2, 9], we achieve 23 + 29 = 52, which is the minimum sum.

We first convert the number into a string to easily access its digits. We then sort the string using qsort. After sorting, we construct two numbers by pairing the smallest and second smallest digits to create new1, and the third and largest digits to create new2. This minimizes their sum, achieving the solution.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1), since we are only dealing with four digits and sorting a fixed size array.
Space Complexity: O(1), no additional space other than some variables.

Try this approach in the editor →

Approach 2: Brute Force Combination

Another approach would be to consider all possible ways to distribute the four digits into two numbers new1 and new2 and calculate their sums. Then select the minimum sum. This method explores all possible combinations.

However, this method is less efficient than sorting because evaluating and summing each combination results in more operations. Sorting ensures better performance and simplicity for this specific problem.

We use a nested loop to iterate over all permutations of the digits, effectively trying every possible split into two numbers. The function computeSum is used to calculate the sum of two formed numbers. Although exhaustive, this ensures finding the minimal sum by brute force.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1), factorial calculations for a small fixed number yield constant operations.
Space Complexity: O(1), since operations are done in-place on an array of constant size.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

Rust

C

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Sorting and Minimal Pairing

Time Complexity: O(1), since we are only dealing with four digits and sorting a fixed size array.
Space Complexity: O(1), no additional space other than some variables.

Brute Force Combination

Time Complexity: O(1), factorial calculations for a small fixed number yield constant operations.
Space Complexity: O(1), since operations are done in-place on an array of constant size.

Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force CombinationO(1)O(1)Good for understanding the search space or verifying correctness when digits are very small in count.
Sorting and Minimal Pairing (Greedy)O(d log d)O(1)Preferred solution. Uses sorting to place the smallest digits in the highest positional weights to minimize total sum.

Video Solution

2160 Minimum Sum of Four Digit Number After Splitting Digits || LeetCode 2160|| Biweekly LeetCode 71Bro Coders5,335 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Minimum Sum of Four Digit Number After Splitting Digits easy or hard?
LeetCode classifies this problem as Easy. The challenge mainly tests basic digit manipulation, sorting, and recognizing a greedy placement strategy rather than complex data structures or algorithms.
Minimum Sum of Four Digit Number After Splitting Digits Python/Java solution
In Python or Java, extract digits using modulo and division, store them in a list or array, sort the digits, then compute (10*d0 + d2) + (10*d1 + d3). The implementation is only a few lines because the greedy arrangement directly gives the minimal sum.
How to solve Minimum Sum of Four Digit Number After Splitting Digits in O(n)?
Since there are only four digits, you can treat the problem as constant time. In a generalized version with more digits, you would extract digits in O(n) and sort them in O(n log n). After sorting, greedily distribute digits between the two numbers to balance place values and minimize the sum.
What is the best approach for Minimum Sum of Four Digit Number After Splitting Digits?
The optimal approach uses greedy sorting. Extract the four digits, sort them, and distribute the smallest digits into the tens place of two numbers. After sorting digits d0 ≤ d1 ≤ d2 ≤ d3, construct numbers (10*d0 + d2) and (10*d1 + d3). This ensures the smallest digits contribute to the highest positional value, minimizing the total sum.
Is Minimum Sum of Four Digit Number After Splitting Digits asked at Google/Amazon/Meta?
This problem represents a typical easy-level greedy and digit manipulation pattern. Variations of digit rearrangement and greedy placement frequently appear in interviews at companies like Amazon and Google, especially in early screening rounds or coding assessments.
What data structure is used in Minimum Sum of Four Digit Number After Splitting Digits?
The solution typically uses a small array or list to store the digits of the number. After extracting digits with modulo and division operations, the array is sorted and used to construct the two resulting numbers.
What is the time complexity of Minimum Sum of Four Digit Number After Splitting Digits?
The greedy solution runs in O(d log d) time due to sorting the digits, where d is the number of digits. Since the problem always has four digits, this effectively becomes constant time in practice. Space complexity is O(1) because only a small fixed array of digits is stored.

Ready to solve this problem?

Practice Minimum Sum of Four Digit Number After Splitting Digits with our built-in code editor and test cases.

Practice on FleetCode