You are given a four-digit number num
. You need to split the digits of num
into two new numbers and calculate the minimum possible sum of these two new numbers.
For example, if num
is 2932
, you can split it into 29
and 32
to get the sum 61
, or into 23
and 92
to get the sum 115
. Your task is to find the combination that results in the minimum possible sum.
num
always a four-digit number?num
’s digits always consist of integers from 0-9?num
into individual digits.Here’s the proposed Python implementation:
def minimumSum(num: int) -> int:
# Step 1: Convert the number into a list of its digits
digits = list(map(int, str(num)))
# Step 2: Sort the digits
digits.sort()
# Step 3: Combine the digits to form the minimized sum
# Smallest number will be one consisting of the smallest and the second smallest digits
num1 = digits[0] * 10 + digits[2]
# Second smallest number will be one consisting of the remaining two digits
num2 = digits[1] * 10 + digits[3]
# Return the sum of these two numbers
return num1 + num2
# Example usage
print(minimumSum(2932)) # Output: 52
num
into a string and mapping each character back to an integer will give us the list of digits.digits[0]
and digits[2]
will form one number, and digits[1]
and digits[3]
will form the other number, ensuring the minimum sum when these numbers are added.This algorithm ensures efficient calculation while maintaining simplicity in handling a fixed small number of digits.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?