✅C++ | ✅Runtime 0 ms | ✅Beats 100 % | Simple Approach
Intuition\n Describe your first thoughts on how to solve this problem. \nMaximum number will be obtained if the value of first digit is changed to 9,ie the firs
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMaximum number will be obtained if the value of first digit is changed to 9,ie the first digit that is not 9 we need to convert it to 9 and hence all of its occurences\nMinimum number will be obtained if the first digits and all of its occurences are changed to 0\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1) Convert the number into string \n2) Find the first digit that is not 9, store its value\n3) Convert all the occurences of that value to 9, and change the string back to integer(let it be maxval)\n4) Now for the minimum value convert all the occurences of first digit to 0,convert the string back to integer(let it be minval)\n5) Return the difference between the two values(maxval-minval).\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int minMaxDifference(int num) {\n        string s=to_string(num);\n        string t=s;\n        int maxval,minval;\n        char val=s[0];\n        char value=val;\n        if(s[0]==\'9\') \n        {\n            for(int i=1;i<s.size();++i){\n                if(s[i]!=\'9\'){\n                    val=s[i];\n                    break;\n                }        \n            }            \n        }\n        for(int i=0;i<s.size();++i){\n            if(s[i]==val){\n                s[i]=\'9\';\n            }\n        }\n        maxval=stoi(s);\n        for(int i=0;i<t.size();++i){\n            if(t[i]==value){\n                t[i]=\'0\';\n            }\n        }\n        minval=stoi(t);\n        return maxval-minval;\n    }\n};\n```
['C++']