simple and Greedy solution, C++, Beginner friendly, well explained
\n# Approach\n1. Convert given number into string to make mapping easy.\n2. Find first character, which does not equal to \'9\' in the string, and store the val
\n# Approach\n1. Convert given number into string to make mapping easy.\n2. Find first character, which does not equal to \'9\' in the string, and store the value at that index, and remap all the indices of stored value with character \'9\' to make maximum number.\n3. Similarly,Find first character, which does not equal to \'0\' in the string, and store the value at that index, and remap all the indices of stored value with character \'0\' to make minimum number.\n4. Return difference of maximum and minimum number.\n\n# Example\n**Example 1:** n = 11891\nstring s = "11891"\nMaximum - Find first index which is not equal to \'9\'.In s, index 0 is not equal to \'9\', so remap all the occurances (values at 0th index)  in the string s with \'9\'. \nSo, max = 11891 -> 99899\nMinimum - Find first index which is not equal to \'0\'.In s, index 0 is not equal to \'0\', so remap all the occurances (values at 0th index)  in the string s with \'0\'. \nSo, min = 11891 -> 00890 = 890\nans = max-min = 99899 - 890 = 99009\n\n**Example 2:** n = 90\nstring s = "90"\nMaximum - Find first index which is not equal to \'9\'.In s, index 1 is not equal to \'9\', so remap all the occurances (values at 1st index)  in the string s with \'9\'. \nSo, max = 90 -> 99\nMinimum - Find first index which is not equal to \'0\'.In s, index 0 is not equal to \'0\', so remap all the occurances (values at 0th index)  in the string s with \'0\'. \nSo, min = 90 -> 00 = 0\nans = max-min = 99 - 0 = 99\n\n\n# Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int minMaxDifference(int num) \n    {\n        string n = to_string(num);\n        string s = n;\n        for(int i=0;i<s.size();i++)\n        {\n            if(s[i]!=\'9\')\n            {\n                char x = s[i];\n                for(int j=i;j<s.size();j++)\n                {\n                    if(s[j]==x)\n                    s[j] = \'9\';\n                }\n                break;\n            }\n        }\n        int h = stoi(s);\n        s = n;\n        for(int i=0;i<s.size();i++)\n        {\n            if(s[i]!=\'0\')\n            {\n                char x = s[i];\n                for(int j=i;j<s.size();j++)\n                {\n                    if(s[j]==x)\n                    s[j] = \'0\';\n                }\n                break;\n            }\n        }\n        int l = stoi(s);\n        return h-l;\n    }\n};\n```
['Greedy', 'C++']