Smallest Value of the Rearranged Number - Problem
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: The sign of the number does not change after rearranging the digits.
Input & Output
Example 1 — Positive Number with Zero
$
Input:
num = 4320
›
Output:
2034
💡 Note:
Sort digits [4,3,2,0] → [0,2,3,4]. Since 0 is first, swap with first non-zero (2) → [2,0,3,4] = 2034
Example 2 — Negative Number
$
Input:
num = -4320
›
Output:
-2034
💡 Note:
Same rearrangement as positive case, but keep negative sign: -2034
Example 3 — Single Digit
$
Input:
num = 0
›
Output:
0
💡 Note:
Single digit 0 remains 0
Constraints
- -231 ≤ num ≤ 231 - 1
Visualization
Tap to expand
Understanding the Visualization
1
Input
Given number with digits to rearrange
2
Process
Sort digits ascending, handle leading zeros
3
Output
Minimum possible value without leading zeros
Key Takeaway
🎯 Key Insight: Sort digits ascending for minimum value, then swap first non-zero to front to avoid leading zeros
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code