Watch 10 video solutions for Minimum Addition to Make Integer Beautiful, a medium level problem involving Math, Greedy. This walkthrough by Bro Coders has 1,498 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.Problem Overview: You are given an integer n and a target digit sum target. The task is to add the smallest possible non‑negative integer x so that the digit sum of n + x becomes less than or equal to target. The result must be the minimum addition that satisfies the constraint.
Approach 1: Incremental Addition and Digit Sum Check (Time: O(k * d), Space: O(1))
The straightforward strategy repeatedly increases the number until its digit sum becomes valid. Start with x = 0, compute the digit sum of n + x, and keep incrementing until the sum is ≤ target. Each iteration recalculates the digit sum by extracting digits using modulo and division. This approach directly simulates the requirement and is easy to implement, but it may require many increments when the next valid number is far away. Time complexity depends on the number of increments k and the digit count d, while space remains constant.
Approach 2: Jump to Nearest Power of Ten (Greedy) (Time: O(d), Space: O(1))
The optimal solution uses a greedy observation about decimal numbers. Instead of incrementing one by one, push the number to the next "clean" boundary where a digit becomes zero after carry propagation. Traverse digits from right to left. Whenever the current digit causes the total digit sum to exceed target, round the number up to the next multiple of 10^k. This effectively sets trailing digits to zero and adds the required carry to the higher position. Continue this process until the digit sum becomes ≤ target. Each step reduces the digit sum significantly, which guarantees only a few iterations proportional to the number of digits.
This greedy rounding works because any minimal addition must eventually eliminate problematic digits from the least significant side. By jumping directly to the next power-of-ten boundary, you skip unnecessary intermediate numbers while preserving the smallest possible increase.
The algorithm uses simple arithmetic operations from math: compute digit sums, track powers of ten, and apply modulo to isolate trailing digits. Because you only scan through the digits of n and occasionally propagate carries, the runtime stays linear in the number of digits.
Recommended for interviews: Interviewers expect the greedy rounding solution. The incremental simulation shows you understand the requirement, but the power-of-ten jump demonstrates number manipulation skills and greedy reasoning. It also achieves optimal O(d) time with O(1) extra space, which is the standard editorial solution.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Incremental Addition and Digit Sum Check | O(k * d) | O(1) | Conceptual brute-force approach or when demonstrating the basic idea before optimization |
| Jump to Nearest Power of Ten (Greedy) | O(d) | O(1) | Best general solution; minimizes additions by rounding digits and propagating carries |