Convert Number To Roman Numeral
Given a number, return its equivalent roman numeral.
● You can find integer to roman conversion rules here.
● Roman numerals are written as largest to smallest from left to right. Example: 12 is written as 10 + 2 hence roman numeral for 12 is X + II = XII.
● Some specific cases follow the subtraction rule instead of addition rule. Example: 4 is written as IV instead of IIII. Here, IV denotes 1 less than 5. Some more cases are:
○ 9 is written as IX.
○ 40 is written as XL and 90 as XC.
○ 400 is written as CD and 900 as CM.
We provided one solution which is the most intuitive and optimised approach.
This problem is an implementation problem. We can divide the given number on the basis of place value. For example:
1942 = 1000 + 900 + 40 + 2 because roman numerals are usually written largest to smallest from left to right. Now, roman numeral for 1942 will be roman numeral of all place values concatenated.
1000 -> M, 900 -> CM, 40 -> XL, 2 -> II
Hence, roman for 1942 will be “MCMXLII”.
We use string arrays to map the place values of (thousands, hundreds, tens, units) to the corresponding roman numeral.
Auxiliary Space Used: