Runtime 0 ms Beats 100% || greedy || must see
Code\n\nclass Solution {\npublic:\n    int minMaxDifference(int num) \n    {\n        //converting the num to the string\n        string str = to_string(num);\n
# Code\n```\nclass Solution {\npublic:\n    int minMaxDifference(int num) \n    {\n        //converting the num to the string\n        string str = to_string(num);\n\n        //maxnum and minnum are not generated\n        string maxnum = str;\n        char maxdigit = \'#\';\n\n        string minnum = str;\n        char mindigit = \'#\';\n\n        //we need to form the max value form the num\n        //we need to form the min value from the num\n        \n        //making the maxnum\n        //iterating over the str and finding the first non 9 number\n        for(int i=0;i<str.length();i++)\n        {\n            if(str[i] != \'9\')\n            {\n                maxdigit = str[i];\n                break;\n            }\n        }\n        if(maxdigit != \'#\')\n        {\n            for(int i=0;i<maxnum.length();i++)\n            {\n                if(maxnum[i] == maxdigit)\n                {\n                    maxnum[i] = \'9\';\n                }\n            }\n        }\n\n        //making the minnum number\n        for(int i=0;i<str.length();i++)\n        {\n            if(str[i] != \'0\')\n            {\n                mindigit = str[i];\n                break;\n            }\n        }\n\n        if(mindigit != \'#\')\n        {\n            for(int i=0;i<minnum.length();i++)\n            {\n                if(minnum[i] == mindigit)\n                {\n                    minnum[i] = \'0\';\n                }\n            }\n        }\n        int val1 = stoi(maxnum);\n        int val2 = stoi(minnum);\n        return val1 - val2;\n\n        \n    }\n};\n```
['Math', 'Greedy', 'C++']