Skip to content

2566. Maximum Difference by Remapping a Digit #1806

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to determine the difference between the maximum and minimum values Bob can obtain by remapping exactly one digit in the given integer num. The remapping involves replacing all occurrences of a chosen digit with another digit. The solution involves two main steps: finding the maximum possible value after remapping and finding the minimum possible value after remapping, then computing their difference.

Approach

  1. Convert the number to a string: This allows us to process each digit individually.
  2. Find the maximum value:
    • Traverse the string from left to right until the first non-'9' digit is encountered.
    • Replace all occurrences of this digit with '9' to maximize the number.
    • If all digi…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Jun 14, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Jun 14, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants