Smallest Number | Easy Code | Java
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public long smallestNumber(long num) {
      String num_str = String.valueOf(num) ;
        String[] numString = new String[num_str.length()];

        for(int i=0; i<numString.length; i++){
            numString[i] = String.valueOf(num_str.charAt(i)) ;
        }
        //negative number
        if(num < 0){
            Arrays.sort(numString, (a, b) -> (b+a).compareTo(a+b));
            String res = String.join("", numString);
            res = res.substring(0, res.length()-1);
            return -Long.parseLong(res) ;
        }
        //positive number
        else {
            Arrays.sort(numString, (a, b) -> (a + b).compareTo(b + a));
            int index = 0 ;
            while(numString.length > 1 && numString[index].equals("0")){
                index++ ;
            }
            String ans = numString[index]; //[index] gives next non zero number
            int countZeros = index ;
            while(countZeros-- > 0){
                ans += "0" ; // add zeroes after one non zero digit
            }

            for(int i=index+1; i<numString.length; i++){
                ans += numString[i] ; // add rest of the non zeroes digit
            }

            return Long.parseLong(ans) ;


        }
    }
}
```
['Java']