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
Rearrange Digits for Minimum ValueInput4320Sort Digits[4,3,2,0] → [0,2,3,4]Swap 0↔2 to avoidleading zeroOutput2034Original digitsin any orderSort ascendingHandle leading zeroSmallest possiblevalid numberWorks for negative numbers too: -4320 → -2034Sign is preserved, only digits are rearranged
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
Asked in
Amazon 25 Microsoft 18 Google 15
28.5K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen