Maximum Difference by Remapping a Digit - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: num = 123
Output: 900
💡 Note: For maximum: replace 1→9 to get 923. For minimum: replace 1→0 to get 023 (which is 23). Difference is 923 - 23 = 900.
Example 2 — All Same Digits
$ Input: num = 555
Output: 888
💡 Note: For maximum: replace 5→9 to get 999. For minimum: replace 5→1 to get 111. Difference is 999 - 111 = 888.
Example 3 — Already Has 9
$ Input: num = 9321
Output: 8019
💡 Note: For maximum: replace 3→9 to get 9921. For minimum: replace 9→1 to get 1321. Difference is 9921 - 1321 = 8600.

Constraints

  • 1 ≤ num ≤ 108

Visualization

Tap to expand
Maximum Difference by Remapping: 123 → ?Input123Strategic RemappingMax: 1→9 gives 923Min: 1→0 gives 023 (= 23)Output900Key Insight: Leftmost digits have maximum impact!Max - Min = 923 - 23 = 900
Understanding the Visualization
1
Input
Given number that can have one digit remapped
2
Process
Find optimal digit replacements for max and min values
3
Output
Return difference between maximum and minimum possible values
Key Takeaway
🎯 Key Insight: Prioritize changing leftmost digits since they have the greatest impact on the number's value
Asked in
Google 15 Microsoft 12 Amazon 8
18.5K Views
Medium Frequency
~15 min Avg. Time
542 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen