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.
Move from brute-force thinking to an efficient approach using math strategy.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Greedy
16 6
467 6
1 1
happy-number)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2457: Minimum Addition to Make Integer Beautiful
class Solution {
public long makeIntegerBeautiful(long n, int target) {
long x = 0;
while (f(n + x) > target) {
long y = n + x;
long p = 10;
while (y % 10 == 0) {
y /= 10;
p *= 10;
}
x = (y / 10 + 1) * p - n;
}
return x;
}
private int f(long x) {
int y = 0;
while (x > 0) {
y += x % 10;
x /= 10;
}
return y;
}
}
// Accepted solution for LeetCode #2457: Minimum Addition to Make Integer Beautiful
func makeIntegerBeautiful(n int64, target int) (x int64) {
f := func(x int64) (y int) {
for ; x > 0; x /= 10 {
y += int(x % 10)
}
return
}
for f(n+x) > target {
y := n + x
var p int64 = 10
for y%10 == 0 {
y /= 10
p *= 10
}
x = (y/10+1)*p - n
}
return
}
# Accepted solution for LeetCode #2457: Minimum Addition to Make Integer Beautiful
class Solution:
def makeIntegerBeautiful(self, n: int, target: int) -> int:
def f(x: int) -> int:
y = 0
while x:
y += x % 10
x //= 10
return y
x = 0
while f(n + x) > target:
y = n + x
p = 10
while y % 10 == 0:
y //= 10
p *= 10
x = (y // 10 + 1) * p - n
return x
// Accepted solution for LeetCode #2457: Minimum Addition to Make Integer Beautiful
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2457: Minimum Addition to Make Integer Beautiful
// class Solution {
// public long makeIntegerBeautiful(long n, int target) {
// long x = 0;
// while (f(n + x) > target) {
// long y = n + x;
// long p = 10;
// while (y % 10 == 0) {
// y /= 10;
// p *= 10;
// }
// x = (y / 10 + 1) * p - n;
// }
// return x;
// }
//
// private int f(long x) {
// int y = 0;
// while (x > 0) {
// y += x % 10;
// x /= 10;
// }
// return y;
// }
// }
// Accepted solution for LeetCode #2457: Minimum Addition to Make Integer Beautiful
function makeIntegerBeautiful(n: number, target: number): number {
const f = (x: number): number => {
let y = 0;
for (; x > 0; x = Math.floor(x / 10)) {
y += x % 10;
}
return y;
};
let x = 0;
while (f(n + x) > target) {
let y = n + x;
let p = 10;
while (y % 10 === 0) {
y = Math.floor(y / 10);
p *= 10;
}
x = (Math.floor(y / 10) + 1) * p - n;
}
return x;
}
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.