2 Approaches, With Comments, Using Array & String Both, Simple & Easy to Understand, [C++]
Implementation\n\n1st Approach\nUsing one extra array\nTime Complexity = O(NLogN) suppose N is :- Total number of digits in num\nSpace Complexity = O(N)\n\n\ncl
Implementation\n\n**1st Approach\nUsing one extra array\nTime Complexity = O(NLogN) suppose N is :- Total number of digits in num\nSpace Complexity = O(N)**\n\n```\nclass Solution {\npublic:\n    long long smallestNumber(long long num) {        \n        vector<long long> arr;\n        \n        // converting num into array\n        while(num){\n            arr.push_back(num%10);\n            num /= 10;\n        }\n        \n        // if num is greater than equal to 0, it means we need to to sort it into ascending order to make it smallest\n        if(num >= 0){\n            sort(arr.begin(), arr.end());\n            \n            // but when we are sorting it into ascending order, then 0 can come at 0th index, so we need to count the total 0, then count+1 index and 0th index we need to swap, to remove the leading 0\'s\n            if(arr.size() >= 2){\n                \n                long long i = 0;\n                \n                while(i < arr.size() && arr[i] == 0) i++;\n                \n                swap(arr[0], arr[i]);\n            }\n        }   // if num is negative, it means we need to to sort it into descending order to make it smallest\n        else{\n            sort(arr.begin(), arr.end(), greater<int>());\n        }\n        \n        // after all process, we need to convert our array back to num to return the result\n        long long n = 0;\n        for(long long itr = 0; itr < arr.size(); itr++){\n            n = n * 10 + arr[itr];\n        }\n        \n        return n;\n    }\n};\n```\n\n\n\n**2nd Approach\nUsing extra string\nTime Complexity = O(NLogN) suppose N is :- Total number of digits in num\nSpace Complexity = O(N), considering string of N size**\n\n```\nclass Solution {\npublic:\n    long long smallestNumber(long long num) {\n        \n        // convert num into string\n        string str = to_string(num);\n        \n        // if num is greater than 0, it means we need to to sort it into ascending order to make it smallest\n        if(num > 0){\n            sort(str.begin(), str.end());\n            \n            // but when we are sorting it into ascending order, then \'0\' can come at 0th index, so we need to count the total \'0\', then count+1 index and 0th index we need to swap, to remove the leading 0\'s\n            int i = 0;                \n            while(i < str.size() && str[i] == \'0\') i++;\n            \n            swap(str[i], str[0]);\n        }   // if num is negative, it means we need to to sort it into descending order to make it smallest\n        else if(num < 0){\n            sort(str.rbegin(), str.rend());\n            return -stoll(str); // convert back into long long and also appending the - sign\n        }\n        return stoll(str);\n    }\n};\n```\nIf you find any issue in understanding the solution then comment below, will try to help you.\nIf you found my solution useful.\nSo **please do upvote and encourage me** to document all leetcode problems\uD83D\uDE03\nHappy Coding :)
['String', 'C', 'Sorting']