Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Integer to Roman Numeral Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Integer to Roman Numeral

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given an integer num, convert it to a roman numeral.

Roman numerals are denoted by a combination of some symbols.

Approach

The idea is to store the numbers (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1) and their roman representation in two different arrays in the same order. Now traverse these arrays and while a value in the array is less than num, append its roman equivalent in the answer string and also subtract the integer value from num. Repeat this process till num is greater than 0.

Analysis

  • Time Complexity: O(1)
  • Auxiliary Space Complexity: O(1)

Implementation

C++
string integerToRoman(int num) {
        vector<string> romanSymbols = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
        vector<int> values = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
	    string roman;
        for (int i = 0; num != 0; i++) {
            while (num >= values[i]) {
                num -= values[i];
                roman += romanSymbols[i];
            }
        }
        return roman;
}
Java
class Solution {
    String integerToRoman(int num) {
        String[] romanSymbols = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
        int[] values = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
        StringBuilder roman = new StringBuilder();
        for (int i = 0; num != 0; i++) {
            while (num >= values[i]) {
                num -= values[i];
                roman.append(romanSymbols[i]);
            }
        }
        return roman.toString();
    }
}
Related Content
Anagrams
Compare Version Numbers
Count And Say
Longest Common Prefix
Longest Palindrome in String
Insert Minimum To Make Palindrome
Reverse Words in String
Roman Numeral to Integer
Substring Search - I
Substring Search - III
Substring Search - II
Substring Search - IV
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet