Maximum Difference by Remapping a Digit


The C++ function minMaxDifference
calculates the difference between the maximum and minimum numbers that can be obtained by replacing a digit in the given number num
.
Here’s how the function works:
It starts by converting the integer
num
to a strings
and creating two copies of this string,large
andsmall
.It initializes two character variables
dl
andds
to1
. These variables will store the digits to be replaced inlarge
andsmall
, respectively.It then enters a loop that iterates over each character in the string
s
.Inside the loop, it first checks whether
dl
is1
and the current character ins
is not'9'
. If so, it setsdl
to the current character and changes the corresponding character inlarge
to'9'
. This operation ensures thatlarge
becomes the largest possible number.For all subsequent characters in
s
that are equal todl
, it also changes the corresponding characters inlarge
to'9'
.It then checks whether
ds
is1
and the current character ins
is not'0'
. If so, it setsds
to the current character and changes the corresponding character insmall
to'0'
. This operation ensures thatsmall
becomes the smallest possible number.For all subsequent characters in
s
that are equal tods
, it also changes the corresponding characters insmall
to'0'
.After the loop, it converts
large
andsmall
back to integers usingstoi
and returns the difference between them.
This function effectively calculates the maximum and minimum numbers that can be obtained by replacing a digit in num
in a single pass, resulting in a time complexity of O(n), where n is the number of digits in num
. The space complexity is also O(n) as it creates strings of size proportional to the number of digits in num
.
Python solution:


Q&A
While coding I am getting an unusual error: {TypeError: ‘int’ object is not subscriptable if digs_min[i] == remap: }
Here it is saying that digs_min is an int datatype. But when I execute print(digs_min), the output comes  [1,1,8,9,1] which is a list. I don’t understand why can’t I access digs_min[0] or digs_min[i] where i is an integer in the program?
This error occurs when you try to index an object that is not indexable. The error message implies that you’re trying to access an element of an integer as if it were a list or a string, which is not allowed in Python.
In your case, you mention that digs_min
is an integer, but the error message suggests that it’s being treated like a list (with the subscripting operation).
If digs_min
is indeed a list as per your output [1,1,8,9,1], then the error may be arising from elsewhere in your code where digs_min
has been unintentionally redefined as an integer.
Please ensure that digs_min
isn’t being reassigned as an integer later in your code. Also, verify that this list isn’t being overwritten or updated in a way that changes its data type to an integer. Doing so would render it unindexable, causing the error you’re seeing.
For more specific help, you may need to provide the section of your code where this error is happening.