leetcodedaybyday - Beats 100% with C++ and Beats 100% with Python3 and Beats 93.17% with Java
IntuitionThe problem requires us to compute themaximum possible numberby replacing one digit in the given number with9throughout the number andminimum possible
## **Intuition**
The problem requires us to compute the **maximum possible number** by replacing one digit in the given number with `9` throughout the number and **minimum possible number** by replacing one digit with `0`. The absolute difference between these two numbers is returned as the result.

---

## **Approach**
1. **Convert the number to a string** representation for easier manipulation.
2. **Find the first non-`9` digit** and replace **all occurrences** of that digit with `9` to get the **maximum** value.
3. **Find the first non-`0` digit** and replace **all occurrences** of that digit with `0` to get the **minimum** value.
4. Convert both the modified numbers back to integers and compute their **difference**.

### **Steps:**
- Iterate through the number to identify the first digit to be replaced.
- Replace all occurrences of the identified digit to obtain the maximum and minimum numbers.
- Compute the final difference.

---

## **Complexity Analysis**
- **Time Complexity:** \( O(n) \), where \( n \) is the number of digits in `num`.
  - Each loop iterates at most **once over the digits** of `num`.
- **Space Complexity:** \( O(n) \) in Java and Python due to the **string manipulation**, but \( O(1) \) in C++ if operations are performed in-place.

---

# Code
```cpp []
class Solution {
public:
    int minMaxDifference(int num) {
        string s = to_string(num);
        for (int i = 0; i < s.length(); i++){
            if (s[i] < '9'){
                for (int j = i + 1; j < s.length(); j++){
                    if (s[i] == s[j]){
                        s[j] = '9';
                    }
                }
                s[i] = '9';
                break;
            }
        }
        int max = stoi(s);
        s = to_string(num);
        for (int i = 0; i < s.length(); i++){
            if (s[i] > '0'){
                for (int j = i + 1; j < s.length(); j++){
                    if (s[i] == s[j]){
                        s[j] = '0';
                    }
                }
                s[i] = '0';
                break;
            }
        }
        return max - stoi(s);
    }
};


```
```java []
class Solution {
    public int minMaxDifference(int num) {
        String s = String.valueOf(num);
        char[] maxArr = s.toCharArray();
        char[] minArr = s.toCharArray();

        for (int i = 0; i < maxArr.length; i++){
            if (maxArr[i] < '9'){
                char replaceChar = maxArr[i];
                for (int j = 0; j < maxArr.length; j++){
                    if (maxArr[j] == replaceChar){
                        maxArr[j] = '9';
                    }
                }
                break;
            }
        }
        for (int i = 0; i < minArr.length; i++){
            if (minArr[i] > '0'){
                char replaceChar = minArr[i];
                for (int j = 0; j < minArr.length; j++){
                    if (minArr[j] == replaceChar){
                        minArr[j] = '0';
                    }
                }
                break;
            }
        }
        int maxNum = Integer.parseInt(new String(maxArr));
        int minNum = Integer.parseInt(new String(minArr));
        
        return maxNum - minNum;
    }
}


```
```python3 []
class Solution:
    def minMaxDifference(self, num: int) -> int:
        numStr = str(num)

        digits = [int(d) for d in list(numStr)]
        n = len(digits)

        d1 = digits[0]
        i = 0
        while (i < n):
            if (digits[i] != 9):
                d1 = digits[i]
                break
            i += 1
        d2 = digits[0]
        i = 0
        while (i < n):
            if (digits[i] != 0):
                d2 = digits[i]
                break
            i += 1
        max_num = int(''.join(['9' if d == d1 else str(d) for d in digits]))
        min_num = int(''.join(['0' if d == d2 else str(d) for d in digits]))

        return max_num - min_num
```


['Math', 'C++', 'Java', 'Python3']