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 an integer num. You know that Bob will sneakily remap one of the 10 possible digits (0 to 9) to another digit.
Return the difference between the maximum and minimum values Bob can make by remapping exactly one digit in num.
Notes:
d1 in num with d2.num does not change.Example 1:
Input: num = 11891 Output: 99009 Explanation: To achieve the maximum value, Bob can remap the digit 1 to the digit 9 to yield 99899. To achieve the minimum value, Bob can remap the digit 1 to the digit 0, yielding 890. The difference between these two numbers is 99009.
Example 2:
Input: num = 90 Output: 99 Explanation: The maximum value that can be returned by the function is 99 (if 0 is replaced by 9) and the minimum value that can be returned by the function is 0 (if 9 is replaced by 0). Thus, we return 99.
Constraints:
1 <= num <= 108Problem summary: You are given an integer num. You know that Bob will sneakily remap one of the 10 possible digits (0 to 9) to another digit. Return the difference between the maximum and minimum values Bob can make by remapping exactly one digit in num. Notes: When Bob remaps a digit d1 to another digit d2, Bob replaces all occurrences of d1 in num with d2. Bob can remap a digit to itself, in which case num does not change. Bob can remap different digits for obtaining minimum and maximum values respectively. The resulting number after remapping can contain leading zeroes.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Greedy
11891
90
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2566: Maximum Difference by Remapping a Digit
class Solution {
public int minMaxDifference(int num) {
String s = String.valueOf(num);
int mi = Integer.parseInt(s.replace(s.charAt(0), '0'));
for (char c : s.toCharArray()) {
if (c != '9') {
return Integer.parseInt(s.replace(c, '9')) - mi;
}
}
return num - mi;
}
}
// Accepted solution for LeetCode #2566: Maximum Difference by Remapping a Digit
func minMaxDifference(num int) int {
s := []byte(strconv.Itoa(num))
first := s[0]
for i := range s {
if s[i] == first {
s[i] = '0'
}
}
mi, _ := strconv.Atoi(string(s))
t := []byte(strconv.Itoa(num))
for i := range t {
if t[i] != '9' {
second := t[i]
for j := i; j < len(t); j++ {
if t[j] == second {
t[j] = '9'
}
}
mx, _ := strconv.Atoi(string(t))
return mx - mi
}
}
return num - mi
}
# Accepted solution for LeetCode #2566: Maximum Difference by Remapping a Digit
class Solution:
def minMaxDifference(self, num: int) -> int:
s = str(num)
mi = int(s.replace(s[0], '0'))
for c in s:
if c != '9':
return int(s.replace(c, '9')) - mi
return num - mi
// Accepted solution for LeetCode #2566: Maximum Difference by Remapping a Digit
impl Solution {
pub fn min_max_difference(num: i32) -> i32 {
let s = num.to_string();
let mi = s
.replace(s.chars().next().unwrap(), "0")
.parse::<i32>()
.unwrap();
for c in s.chars() {
if c != '9' {
let mx = s.replace(c, "9").parse::<i32>().unwrap();
return mx - mi;
}
}
num - mi
}
}
// Accepted solution for LeetCode #2566: Maximum Difference by Remapping a Digit
function minMaxDifference(num: number): number {
const s = num.toString();
const mi = +s.replaceAll(s[0], '0');
for (const c of s) {
if (c !== '9') {
const mx = +s.replaceAll(c, '9');
return mx - mi;
}
}
return num - mi;
}
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.