O(n) C++ Solution using Hashmap (0 ms)
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n\n# Code\n```\nclass Solution {\npublic:\n    long long smallestNumber(long long num) {\n        long long ans = 0;\n        unordered_map<int, int> mp; // counter for diff digits and their freq\n        bool neg = false;\n\n        if (num < 0) {\n            neg = true;\n            num = abs(num);\n        }\n\n        // get num of freq\n        while(num > 0) {\n            int temp = num % 10;\n            mp[temp]++;\n            num /= 10;\n        }\n\n        // minimize num if +ve\n        //  first start with smallest non 0 digit\n        //  add all zeroes\n        //  add all remaining numbers in increasing order\n        if (!neg) {\n            for (int i = 1; i < 10; i++) {\n                if (mp[i] != 0) {\n                    ans = ans * 10 + i;\n                    mp[i]--;\n                    break;\n                }\n            }\n            while(mp[0] != 0) {\n                ans = ans * 10;\n                mp[0]--;\n            }\n\n            for (int i = 0; i < 10; i++) {\n                while(mp[i] != 0) {\n                    ans = ans * 10 + i;\n                    mp[i]--;\n                }\n            }\n        }\n\n        // minimuze num if -ve\n        //  add digits in decreasing order then turn it negative\n\n        else {\n            for (int i = 9; i >= 0; i--) {\n                while(mp[i] != 0) {\n                    ans = ans * 10 + i;\n                    mp[i]--;\n                }\n            }\n            ans = ans * (-1);\n        }\n\n        return ans;\n    }\n};\n```
['C++']