C++ Easy Solution | Beginner Friendly | Easy to Understand 👍
Approach\n- First of all, count the occurence of each digit and store them in an array. Now, if the number is negitive, then multiply the number by -1 and store
# Approach\n- First of all, count the occurence of each digit and store them in an array. Now, if the number is negitive, then multiply the number by -1 and store in a new variable. \n\n- If the number is negitive, reverse iterate through the occurence array. If the value is not 0, add it to the number using the formula:\n**ans = (ans * 10) + i**. Also, remember to add the element till the value at the index becomes 0. \n\n- If the number is positive, for the first digit of the number, iterate through the array and add the digit at whatever index the value is not 0. Then, iterate it normally and keep adding the digits. \n\n\uD83D\uDC68\u200D\uD83D\uDCBB Friend\'s if you find this solution helpful \uD83D\uDE0A, PLEASE do UPVOTE. By doing that motivates me to create a better post like this \u270D\uFE0F\n\n\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n# Code\n```\nclass Solution {\npublic:\n    long long smallestNumber(long long num) {\n        int arr[10] = {0};\n        long long ans = 0;\n        long long n = num;\n        if(num < 0) {\n            n = n * -1;\n        }\n        while(n != 0) {\n            int digit = n%10;\n            arr[digit]++;\n            n /= 10;\n        }\n        if(num < 0) {\n            for(int i=9; i>=0; i--) {\n                if(arr[i] != 0) {\n                    while(arr[i]) {\n                        ans = (ans * 10) + i;\n                        arr[i]--;\n                    }\n                }\n            }\n            ans *= -1;\n            return ans;\n        }\n        else {\n            for(int i=1; i<10; i++) {\n                if(arr[i] != 0) {\n                    ans = (ans * 10) + i;\n                    arr[i]--;\n                    break;\n                }\n            }\n            for(int i=0; i<10; i++) {\n                if(arr[i] != 0) {\n                    while(arr[i]) {\n                        ans = (ans * 10) + i;\n                        arr[i]--;\n                    }\n                }\n            }\n            return ans;\n        }\n    }\n};\n```
['Array', 'Math', 'String', 'Sorting', 'C++']