2165. Smallest Value of the Rearranged Number LeetCode Solution
In this guide, you will get 2165. Smallest Value of the Rearranged Number LeetCode Solution with the best time and space complexity. The solution to Smallest Value of the Rearranged Number problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Smallest Value of the Rearranged Number solution in C++
- Smallest Value of the Rearranged Number solution in Java
- Smallest Value of the Rearranged Number solution in Python
- Additional Resources

Problem Statement of Smallest Value of the Rearranged Number
You are given an integer num. Rearrange the digits of num such that its value is minimized and it does not contain any leading zeros.
Return the rearranged number with minimal value.
Note that the sign of the number does not change after rearranging the digits.
Example 1:
Input: num = 310
Output: 103
Explanation: The possible arrangements for the digits of 310 are 013, 031, 103, 130, 301, 310.
The arrangement with the smallest value that does not contain any leading zeros is 103.
Example 2:
Input: num = -7605
Output: -7650
Explanation: Some possible arrangements for the digits of -7605 are -7650, -6705, -5076, -0567.
The arrangement with the smallest value that does not contain any leading zeros is -7650.
Constraints:
-1015 <= num <= 1015
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2165. Smallest Value of the Rearranged Number LeetCode Solution in C++
class Solution {
public:
long long smallestNumber(long long num) {
string s = to_string(abs(num));
ranges::sort(s, [&](int a, int b) { return num < 0 ? a > b : a < b; });
if (num > 0)
swap(s[0], s[s.find_first_not_of('0')]);
return stoll(s) * (num < 0 ? -1 : 1);
}
};
/* code provided by PROGIEZ */
2165. Smallest Value of the Rearranged Number LeetCode Solution in Java
class Solution {
public long smallestNumber(long num) {
String[] digits = String.valueOf(Math.abs(num)).split("");
String s = Stream.of(digits).sorted().collect(Collectors.joining());
StringBuilder sb = new StringBuilder(s);
if (num <= 0)
return -1 * Long.parseLong(sb.reverse().toString());
if (sb.charAt(0) == '0') {
final int firstNonZeroIndex = sb.lastIndexOf("0") + 1;
sb.setCharAt(0, sb.charAt(firstNonZeroIndex));
sb.setCharAt(firstNonZeroIndex, '0');
}
return Long.parseLong(sb.toString());
}
}
// code provided by PROGIEZ
2165. Smallest Value of the Rearranged Number LeetCode Solution in Python
class Solution:
def smallestNumber(self, num: int) -> int:
s = sorted(str(abs(num)), reverse=num < 0)
firstNonZeroIndex = next((i for i, c in enumerate(s) if c != '0'), 0)
s[0], s[firstNonZeroIndex] = s[firstNonZeroIndex], s[0]
return int(''.join(s)) * (-1 if num < 0 else 1)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.