You are given two positive integers n and target.
An integer is considered beautiful if the sum of its digits is less than or equal to target.
Return the minimum non-negative integer x such that n + x is beautiful. The input will be generated such that it is always possible to make n beautiful.
Example 1:
Input: n = 16, target = 6 Output: 4 Explanation: Initially n is 16 and its digit sum is 1 + 6 = 7. After adding 4, n becomes 20 and digit sum becomes 2 + 0 = 2. It can be shown that we can not make n beautiful with adding non-negative integer less than 4.
Example 2:
Input: n = 467, target = 6 Output: 33 Explanation: Initially n is 467 and its digit sum is 4 + 6 + 7 = 17. After adding 33, n becomes 500 and digit sum becomes 5 + 0 + 0 = 5. It can be shown that we can not make n beautiful with adding non-negative integer less than 33.
Example 3:
Input: n = 1, target = 1 Output: 0 Explanation: Initially n is 1 and its digit sum is 1, which is already smaller than or equal to target.
Constraints:
1 <= n <= 10121 <= target <= 150n beautiful.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To solve this problem using a brute force approach, we can simply start by looking at our original number and checking if the sum of its digits is already small enough. If it is not, we can test adding one to our number repeatedly, checking the sum of the digits after each addition until we finally reach a number whose digit sum meets our goal.
Here's how the algorithm would work step-by-step:
def make_integer_beautiful(original_number, target_limit):
# We need to track how much we have added to reach our goal
total_added_amount = 0
current_number = original_number
while True:
digit_sum = sum(int(digit_character) for digit_character in str(current_number))
# Stop immediately if our current digit sum is already small enough
if digit_sum <= target_limit:
return total_added_amount
current_number += 1
total_added_amount += 1
The optimal strategy works by looking at the digits of the number from right to left, similar to how we perform addition on paper. Whenever the sum of the digits exceeds the allowed limit, we bump up the value of the next digit to round up the number, which naturally forces the smaller digits to become zeros and reduces the overall sum.
Here's how the algorithm would work step-by-step:
class Solution:
def make_integer_beautiful(self, initial_number: int, target_sum: int) -> int:
original_number = initial_number
multiplier = 1
# We calculate the sum of digits repeatedly until it satisfies the target constraint.
while sum(int(digit) for digit in str(initial_number)) > target_sum:
initial_number = (initial_number // (multiplier * 10) + 1) * (multiplier * 10)
multiplier *= 10
# The difference between the rounded number and original gives the minimum addition.
return initial_number - original_number| Case | How to Handle |
|---|---|
| n is already beautiful where the sum of its digits is less than or equal to target | The algorithm should immediately check this condition and return 0 as no addition is needed. |
| n and target are single-digit numbers | A direct comparison handles this trivially by returning the difference or zero if already within target. |
| Maximum possible inputs where n is up to 10 to the power of 12 and target is small | Using 64-bit integer types prevents overflow during iterative addition and digit sum calculations. |
| Trailing zeros in n causing cascading carries when adding x | Incrementally rounding up n to the next power of 10 effectively clears trailing digits and generates carries correctly. |
| Target is very small like 1 while n is large | The iterative carry propagation mechanism scales up the number place by place until the most significant digit satisfies the target sum. |
| n contains all nines leading to maximum possible digit sum | The carry operation smoothly propagates across all nines, adding a leading one and resetting lower digits to zero. |
| Negative numbers or zero as inputs | The problem constraints specify positive integers for n and target, but standard defensive programming validates inputs against non-positive values. |