Simple Answer Beats 95%
\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
\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 {\n    public int minMaxDifference(int num) {\n        //mapping the first non nine digit to 9 gives us the maximum number\n        //mapping the first non zero digit to 0 gives us the minimum number\n        String max, min, hold = Integer.toString(num);\n        char check = \'a\';\n        boolean flag = true;\n        char[] array = hold.toCharArray();\n        //calculating the maximum number\n        for(int i = 0; i<array.length; i++){\n            if(array[i] == check){\n                array[i] = \'9\';\n                continue;\n            }\n            if(flag && array[i]!=\'9\'){\n                check = array[i];\n                array[i] = \'9\';\n                flag = false;\n            }\n        }\n        max = String.valueOf(array);\n        hold = Integer.toString(num);\n        check = \'a\';\n        flag = true;\n        array = hold.toCharArray();\n        //calculating the minimum number\n        for(int i = 0; i<array.length; i++){\n            if(array[i] == check){\n                array[i] = \'0\';\n                continue;\n            }\n            if(flag && array[i]!=\'0\'){\n                check = array[i];\n                array[i] = \'0\';\n                flag = false;\n            }\n        }\n        min = String.valueOf(array);\n        return Integer.parseInt(max) - Integer.parseInt(min);\n    }\n}\n```
['Java']