C++ Solution | | 100% Beats | | Easy approach
Intuition
The goal is to rearrange the digits of a given number to form the smallest possible number. For positive numbers, this means sorting the digits in asc
Intuition
The goal is to rearrange the digits of a given number to form the smallest possible number. For positive numbers, this means sorting the digits in ascending order. For negative numbers, this means sorting the digits in descending order and then negating the result.

Approach
Convert the number to a string to easily manipulate its digits.

If the number is positive:

Sort the digits in ascending order.

Ensure that the first digit is not zero by swapping it with the first non-zero digit if necessary.

If the number is negative:

Convert the number to its positive equivalent.

Sort the digits in descending order.

Convert the sorted string back to a number and negate it.

Convert the sorted string back to a number using stoll to handle large numbers.

Complexity
Time complexity: $$O(n \log n)$$, where $$n$$ is the number of digits in the number. This is due to the sorting step.

Space complexity: $$O(n)$$, where $$n$$ is the number of digits in the number. This is due to the storage of the string representation of the number.

# Code
```cpp []
class Solution {
public:
    long long smallestNumber(long long num) {
        long long res;
        string s;
        if(num>0){
            s=to_string(num);
            sort(s.begin(),s.end());
            if(s[0]=='0'){
                for(int i=1;i<s.size();i++){
                    if(s[i]!='0'){
                        swap(s[0],s[i]);
                        break;
                    }
                }
            }
            res=stoll(s);
        } else{
            num=-num;
            s=to_string(num);
            sort(s.begin(),s.end(),greater<char>());
            res=stoll(s);
            res=-res;
        }
        return res;
    }
};
```
['C++']