Integer to Roman Numeral - Amazon Top Interview Questions


Problem Statement :


Given an integer n, return its corresponding Roman numeral.

Roman numerals contain the symbols representing values in the following list:

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

Symbols are typically written largest to smallest, from left to right, and can be computed by summing the values of all the symbols. However, in some cases, when a symbol of lower value is to the left of a symbol of higher value, then the lower value is subtracted from the higher one.

There are 6 cases where this is possible:

When "I" is before "V", we get 4.
When "I" is before "X", we get 9.
When "X" is before "L", we get 40.
When "X" is before "C", we get 90.
When "C" is before "D", we get 400.
When "C" is before "M", we get 900.

Roman numerals must also follow these rules:

No symbol is repeated more than 3 times.

The symbols "V", "L", and "D" are not repeated.

Constraints

1 ≤ n ≤ 3000

Example 1

Input

n = 12

Output

"XII"

Explanation

"XII" = 10 + 1 + 1 = 12

Example 2

Input

n = 14

Output

"XIV"

Explanation

"XIV" = 10 + 4 = 14



Solution :



title-img




                        Solution in C++ :

string solve(int num) {
    string thousands[] = {"", "M", "MM", "MMM"};  // as num is lesser than 3000
    string hundreds[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
    string tens[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
    string ones[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};

    string str = thousands[num / 1000] + hundreds[(num % 1000) / 100] + tens[(num % 100) / 10] +
                 ones[num % 10];
    return str;
}
                    




                        Solution in Python : 
                            
class Solution:
    def solve(self, n):
        res = ""
        table = [
            (1000, "M"),
            (900, "CM"),
            (500, "D"),
            (400, "CD"),
            (100, "C"),
            (90, "XC"),
            (50, "L"),
            (40, "XL"),
            (10, "X"),
            (9, "IX"),
            (5, "V"),
            (4, "IV"),
            (1, "I"),
        ]
        for cap, roman in table:
            d, m = divmod(n, cap)
            res += roman * d
            n = m

        return res
                    


View More Similar Problems

Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →

Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →

Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →

Jim and the Skyscrapers

Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space

View Solution →

Palindromic Subsets

Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a . Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms: 1 i j t: All letters in the inclusive range from i t

View Solution →