Minimum Sum of Four Digit Number After Splitting Digits - Problem
Minimum Sum After Digit Redistribution
Imagine you have a 4-digit number and you need to redistribute its digits to create two new numbers that give you the smallest possible sum.
๐ฏ Your Mission: Given a positive integer
๐ Rules:
โข You must use all four digits from the original number
โข Leading zeros are allowed (so digits can be distributed freely)
โข Each new number must have at least one digit
๐ก Example: For
Available digits: [2, 9, 3, 2]
Possible splits: [22, 93], [23, 92], [29, 32], [2, 932], etc.
The optimal split minimizes the sum!
Imagine you have a 4-digit number and you need to redistribute its digits to create two new numbers that give you the smallest possible sum.
๐ฏ Your Mission: Given a positive integer
num with exactly 4 digits, split all its digits to form two new integers new1 and new2. Your goal is to minimize new1 + new2.๐ Rules:
โข You must use all four digits from the original number
โข Leading zeros are allowed (so digits can be distributed freely)
โข Each new number must have at least one digit
๐ก Example: For
num = 2932Available digits: [2, 9, 3, 2]
Possible splits: [22, 93], [23, 92], [29, 32], [2, 932], etc.
The optimal split minimizes the sum!
Input & Output
example_1.py โ Basic Case
$
Input:
num = 2932
โบ
Output:
52
๐ก Note:
Digits are [2,9,3,2]. After sorting: [2,2,3,9]. Optimal split: new1 = 2*10 + 3 = 23, new2 = 2*10 + 9 = 29. Sum = 23 + 29 = 52.
example_2.py โ All Same Digits
$
Input:
num = 4009
โบ
Output:
13
๐ก Note:
Digits are [4,0,0,9]. After sorting: [0,0,4,9]. Optimal split: new1 = 0*10 + 4 = 4, new2 = 0*10 + 9 = 9. Sum = 4 + 9 = 13.
example_3.py โ Ascending Order
$
Input:
num = 1234
โบ
Output:
37
๐ก Note:
Digits are [1,2,3,4]. Already sorted. Optimal split: new1 = 1*10 + 3 = 13, new2 = 2*10 + 4 = 24. Sum = 13 + 24 = 37.
Visualization
Tap to expand
Understanding the Visualization
1
Sort Your Cards
Arrange the 4 digit cards in ascending order
2
Strategic Placement
Give the smallest cards the 'tens' position (higher weight)
3
Fill Remaining
Place larger cards in 'ones' positions
4
Calculate Score
Sum both hands for minimum total
Key Takeaway
๐ฏ Key Insight: Always put the smallest digits in the tens places to minimize both numbers simultaneously!
Time & Space Complexity
Time Complexity
O(1)
Fixed 4 digits means constant number of combinations (about 2^4 = 16 ways)
โ Linear Growth
Space Complexity
O(1)
Only storing digits and minimum sum, no additional data structures
โ Linear Space
Constraints
- 1000 โค num โค 9999
- num consists of exactly 4 digits
- Leading zeros are allowed in the resulting numbers
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code