Logic | Simple solution with 100% beaten
IntuitionThe task is to maximize and minimize a number by making specific replacements in its digits. The goal is to determine the difference between the larges
# Intuition
The task is to maximize and minimize a number by making specific replacements in its digits. The goal is to determine the difference between the largest possible number and the smallest possible number that can be formed.

# Approach
- Convert the number to a string for easy manipulation of individual digits.
- Find the maximum number:
Traverse the string and locate the first digit that is not '9'.
Replace this digit and all its subsequent occurrences in the string with '9'.
- Find the minimum number:
Identify the first digit of the original number.
Replace all occurrences of this digit in the string with '0'.
- Convert the modified strings back to integers and calculate the difference between the maximum and minimum numbers.
# Complexity
- Time complexity: O(n)

- Space complexity: O(n)

# Code
```cpp []
class Solution {
public:
    int minMaxDifference(int num) {
        // Convert the number to a string
        string original = to_string(num);
        string maxNum = original;
        string minNum = original;

        // Create the maximum number by replacing the first non-'9' digit
        for (char& digit : maxNum) {
            if (digit != '9') {
                char toReplace = digit;
                for (char& d : maxNum) {
                    if (d == toReplace) {
                        d = '9';
                    }
                }
                break;
            }
        }

        // Create the minimum number by replacing all occurrences of the first digit with '0'
        char firstDigit = minNum[0];
        for (char& digit : minNum) {
            if (digit == firstDigit) {
                digit = '0';
            }
        }

        // Convert the modified strings back to integers and calculate the difference
        return stoi(maxNum) - stoi(minNum);
    }
};

```
['C++']