Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
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.
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 <= 9999Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Greedy
2932
4009
add-digits)difference-between-element-sum-and-digit-sum-of-an-array)alternating-digit-sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2160: Minimum Sum of Four Digit Number After Splitting Digits
class Solution {
public int minimumSum(int num) {
int[] nums = new int[4];
for (int i = 0; num != 0; ++i) {
nums[i] = num % 10;
num /= 10;
}
Arrays.sort(nums);
return 10 * (nums[0] + nums[1]) + nums[2] + nums[3];
}
}
// Accepted solution for LeetCode #2160: Minimum Sum of Four Digit Number After Splitting Digits
func minimumSum(num int) int {
var nums []int
for num > 0 {
nums = append(nums, num%10)
num /= 10
}
sort.Ints(nums)
return 10*(nums[0]+nums[1]) + nums[2] + nums[3]
}
# Accepted solution for LeetCode #2160: Minimum Sum of Four Digit Number After Splitting Digits
class Solution:
def minimumSum(self, num: int) -> int:
nums = []
while num:
nums.append(num % 10)
num //= 10
nums.sort()
return 10 * (nums[0] + nums[1]) + nums[2] + nums[3]
// Accepted solution for LeetCode #2160: Minimum Sum of Four Digit Number After Splitting Digits
impl Solution {
pub fn minimum_sum(mut num: i32) -> i32 {
let mut nums = [0; 4];
for i in 0..4 {
nums[i] = num % 10;
num /= 10;
}
nums.sort();
10 * (nums[0] + nums[1]) + nums[2] + nums[3]
}
}
// Accepted solution for LeetCode #2160: Minimum Sum of Four Digit Number After Splitting Digits
function minimumSum(num: number): number {
const nums = new Array(4).fill(0);
for (let i = 0; i < 4; i++) {
nums[i] = num % 10;
num = Math.floor(num / 10);
}
nums.sort((a, b) => a - b);
return 10 * (nums[0] + nums[1]) + nums[2] + nums[3];
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.