Smallest Value of the Rearranged number easy solution in c++ optimized
\n# Approach\n follow below steps:\n1)convert the nums to string using(to_string())function\n2)sort the string in non-decreasing order and negative in non-incre
\n# Approach\n follow below steps:\n1)convert the nums to string using(to_string())function\n2)sort the string in non-decreasing order and negative in non-increasing order\n3)swap the value if its leading 0 \n4)now stoll(s) converts to long and obviously multiply negative nums by -1 at the end return the minimize ans;\n\n# Complexity\n- Time complexity:\n- O(nlogn)\n- given there is maximum 15 digit so worstly it is O(15log15) \n \n\n- Space complexity:\n- O(logn)\n\n# Code\n```\nclass Solution {\npublic:\n    long long smallestNumber(long long num) {\n        string s=to_string(abs(num));\n        if(num>=0)sort(s.begin(),s.end());\n        else sort(s.begin(),s.end(),greater<char>());\n        if(num>0){\n            int ind=s.find_first_not_of(\'0\');\n            swap(s[0],s[ind]);\n        }\n\n        long ans=stoll(s);\n        if(num<0){\n            ans*=-1;\n        }\n                 \n        return ans;\n    }\n};\n```
['C++']