Java solution using count array (explained with comments)
Use count array to maintain count of each digit. \nUsing the count array form the rearranged number again.\nIn case of positive number, make sure the minimum di
Use count array to maintain count of each digit. \nUsing the count array form the rearranged number again.\nIn case of positive number, make sure the minimum digit with non zero count comes first and then other digits can be included whose count are non zero (including the digit \'0\').\n\nIn case of negative number, count can be considered in reverse order, with count of digit 9 considered first, then 8 and likewise.\n\nBelow is the implementation of the approach:\n```\nclass Solution {\n    public long smallestNumber(long num) {\n        int[] count = new int[10];\n        long tempNum = num > 0 ? num : num * -1;\n        int min = 10;\n\t\t// get the count of each digit\n        while (tempNum > 0) {\n            int rem = (int)(tempNum % 10);\n            if (rem != 0) {\n                min = Math.min(min, rem);\n            }\n            count[rem]++;\n            tempNum = tempNum / 10;\n        }\n\t\t\n        long output = 0;\n\t\t// positive number\n        if (num > 0) {\n            \n            output = output * 10 + min; // add the min non zero digit first in the output\n            count[min]--;  //decrement the count of that digit\n         \t// iterate all the digits from ) to 9 and get the count value to form the number\n            for (int i = 0; i < 10; i++) {\n                for (int j = 0; j < count[i]; j++) {\n                    output = output * 10 + i;\n                }\n            }\n        } else {  //negative number\n\t\t   //iterate in reverse order (from 9 to 0)\n            for (int i = 9; i >= 0; i--) {\n                for (int j = 0; j < count[i]; j++) {\n                    output = output * 10 + i;\n                }\n            }\n            output = output * -1; //multiply with -1 to make output negative\n        }\n        return output;\n    }\n}\n```\n\nSpace Complexity : O(k) k = 10, overall its a constant space.\nTime Complexity : O(logN) : N is the given input.\n
['Counting Sort', 'Java']