Maximum Difference by Remapping a Digit

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
    int minMaxDifference(int num) {
        string s =to_string(num), large = to_string(num), small = to_string(num);
        char dl = -1, ds = -1;
        for(int i = 0; i < s.size(); ++i){
            if(dl == -1 && s[i] != '9'){
                dl = s[i];
                large[i] = '9';
            }else {
                large[i] = (s[i] == dl)?'9': s[i];
            }

            if(ds == -1 && s[i] != '0'){
                ds = s[i];
                small[i] = '0';
            }else{
                small[i] = (s[i] == ds)?'0':s[i];
            }
        }
        return stoi(large) - stoi(small);
    }
};

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:

  1. It starts by converting the integer num to a string s and creating two copies of this string, large and small.

  2. It initializes two character variables dl and ds to -1. These variables will store the digits to be replaced in large and small, respectively.

  3. It then enters a loop that iterates over each character in the string s.

  4. Inside the loop, it first checks whether dl is -1 and the current character in s is not '9'. If so, it sets dl to the current character and changes the corresponding character in large to '9'. This operation ensures that large becomes the largest possible number.

  5. For all subsequent characters in s that are equal to dl, it also changes the corresponding characters in large to '9'.

  6. It then checks whether ds is -1 and the current character in s is not '0'. If so, it sets ds to the current character and changes the corresponding character in small to '0'. This operation ensures that small becomes the smallest possible number.

  7. For all subsequent characters in s that are equal to ds, it also changes the corresponding characters in small to '0'.

  8. After the loop, it converts large and small back to integers using stoi 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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
    def minMaxDifference(self, num: int) -> int:
        s = str(num)
        large = list(s)
        small = list(s)
        dl, ds = '-1', '-1'

        for i in range(len(s)):
            if dl == '-1' and s[i] != '9':
                dl = s[i]
                large[i] = '9'
            else:
                large[i] = '9' if s[i] == dl else s[i]

            if ds == '-1' and s[i] != '0':
                ds = s[i]
                small[i] = '0'
            else:
                small[i] = '0' if s[i] == ds else s[i]

        return int(''.join(large)) - int(''.join(small))

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 re-assigned 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 un-indexable, 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.