Integer to Roman - Problem

Convert Integer to Roman Numerals

Roman numerals are an ancient number system that's still used today - from clock faces to movie sequels! Your task is to convert a modern integer into its ancient Roman numeral equivalent.

The Roman numeral system uses seven symbols:

SymbolValue
I1
V5
X10
L50
C100
D500
M1000

Special Rules:

  • Generally, symbols are combined from largest to smallest (left to right)
  • When a smaller numeral appears before a larger one, it means subtraction
  • Only specific subtractive combinations are allowed: IV (4), IX (9), XL (40), XC (90), CD (400), CM (900)
  • You can't repeat a symbol more than 3 times consecutively

Examples: 3 → III, 4 → IV, 9 → IX, 58 → LVIII, 1994 → MCMXCIV

Input & Output

example_1.py — Basic Conversion
$ Input: num = 3
Output: "III"
💡 Note: 3 is represented as three I's in Roman numerals: I + I + I = III
example_2.py — Subtractive Case
$ Input: num = 58
Output: "LVIII"
💡 Note: 58 = 50 + 5 + 3 = L + V + III = LVIII
example_3.py — Complex Number
$ Input: num = 1994
Output: "MCMXCIV"
💡 Note: 1994 = 1000 + 900 + 90 + 4 = M + CM + XC + IV = MCMXCIV (uses multiple subtractive forms)

Constraints

  • 1 ≤ num ≤ 3999
  • Input is guaranteed to be a valid integer
  • Note: Roman numerals don't have a symbol for zero

Visualization

Tap to expand
Roman Numeral Conversion: Like an Ancient Cash Register1000M900CM500D400CD100CConverting 1994: Start with largest denominationStep 1: 1994 ÷ 1000 = 1 remainder 994 → Take 1×M coin → Result: "M"Step 2: 994 ÷ 900 = 1 remainder 94 → Take 1×CM coin → Result: "MCM"Step 3: 94 ÷ 90 = 1 remainder 4 → Take 1×XC coin → Result: "MCMXC"Step 4: 4 ÷ 4 = 1 remainder 0 → Take 1×IV coin → Result: "MCMXCIV"Final Roman Numeral:MCMXCIV
Understanding the Visualization
1
Prepare Coin Types
Set up Roman 'coins' including special subtractive combinations (IV=4, IX=9, etc.)
2
Start with Largest
Always try to use the largest possible Roman value first
3
Calculate Quantity
Use division to find how many of each 'coin' type to use
4
Update Remainder
Use modulo to find what's left after using current coin type
5
Repeat Process
Continue with next largest coin type until remainder is zero
Key Takeaway
🎯 Key Insight: Pre-computing all valid Roman combinations (including subtractive forms) and processing them in descending order allows for an elegant greedy algorithm that builds the optimal Roman numeral representation in a single pass.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28 Apple 22
89.5K Views
Medium-High 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