Integer to Roman - Problem

Convert an integer to its Roman numeral representation using the seven basic symbols:

Symbol Values:

  • I = 1
  • V = 5
  • X = 10
  • L = 50
  • C = 100
  • D = 500
  • M = 1000

Conversion Rules:

  • Use largest possible values first (greedy approach)
  • Special subtractive cases: IV (4), IX (9), XL (40), XC (90), CD (400), CM (900)
  • Only append symbols consecutively up to 3 times (I, X, C, M)
  • Never repeat V, L, D multiple times

Given an integer between 1 and 3999, return its Roman numeral string.

Input & Output

Example 1 — Standard Conversion
$ Input: num = 3
Output: III
💡 Note: 3 is represented as III (three ones: I + I + I = III)
Example 2 — Subtractive Case
$ Input: num = 58
Output: LVIII
💡 Note: 58 = 50 + 8 = L + VIII (50 as L, 8 as V + III)
Example 3 — Complex Number
$ Input: num = 1994
Output: MCMXCIV
💡 Note: 1994 = 1000 + 900 + 90 + 4 = M + CM + XC + IV

Constraints

  • 1 ≤ num ≤ 3999

Visualization

Tap to expand
Integer to Roman Numeral Conversion1994Input IntegerGreedyConversion1000→M900→CM, etc.MCMXCIVRoman NumeralProcess: Use largest values first (M=1000, CM=900, XC=90, IV=4)1994 → 1000 + 900 + 90 + 4 → M + CM + XC + IV
Understanding the Visualization
1
Input
Integer number (e.g., 1994)
2
Process
Use value-symbol pairs greedily from largest to smallest
3
Output
Roman numeral string (MCMXCIV)
Key Takeaway
🎯 Key Insight: Pre-include subtractive cases (IV, IX, XL, etc.) as single units to enable simple greedy selection
Asked in
Microsoft 45 Amazon 38
78.0K Views
Medium Frequency
~15 min Avg. Time
2.8K 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