👇Very Easy solution in C++
Approach\nAprroach is very simple.\nFirst convert the number in string using string function: to_string()\nReason: To manipulate digits as we wants.\n// For Pos
# Approach\nAprroach is very simple.\nFirst convert the number in string using string function: to_string()\nReason: To manipulate digits as we wants.\n// For Positive number\nif number is positive then accending order of digit will be our minimum number.Therefore, sort the string in accending order.\nExample: 1234 < 4213\nBut in this questions leading zero not allowed.\nTo solve this we need to swap the first zero to another smallest number after zero.\nThen convert string to again number using function: stoll()\n(stoll() : s-->string, to-->to, ll-->long long)\n//For Negative number\nif number is nagative then deccending order of digit will be our minimum number becuase in series of negative number largest number with nagative sign will be smallest number.\nTherefore, sort the string in deccending order.\nExample: -4321 < -3214 \nThen convert string to again number using function: stoll()\n# Complexity\n- Time complexity:\nO(nlog(n))\n\uD83D\uDC47\nto_string()-->O(n)\nsort()-->O(nlog(n))\nstoll()-->O(n)\nOverall: O(nlog(n))\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution {\npublic:\n    long long smallestNumber(long long num) {\n      //To handle single digit number\n       if(num%10==num)\n        return num;\n      // Positve number in accending order of digit form smallest number\n       if(num>0)\n       {\n         string s=to_string(num);\n         sort(s.begin(),s.end());\n      // To handle leading zeros \n           if(s[0]==\'0\')\n           {\n              int i=0;\n              while(i<s.size()-1&&s[i]==s[i+1])\n                i++; \n               char temp=s[0];\n               s[0]=s[i+1];\n               s[i+1]=temp;\n           }\n          return stoll(s);\n       } \n     // Negative number in deccending order of digit form smallest number\n      num=abs(num);\n      string s=to_string(num);\n      sort(s.begin(),s.end(),greater<>());\n      return -stoll(s);\n    }\n};\n```
['Math', 'Sorting', 'C++']