Professional Documents
Culture Documents
For example, 2 is written as II in Roman numeral, just two one's added together. 12 is
written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V
+ II.
Roman numerals are usually written largest to smallest from left to right. However,
the numeral for four is not IIII. Instead, the number four is written as IV. Because the
one is before the five we subtract it making four. The same principle applies to the
number nine, which is written as IX. There are six instances where subtraction is used:
Example 1:
Input: s = "III"
Output: 3
Example 2:
Input: s = "IV"
Output: 4
Example 3:
Input: s = "IX"
Output: 9
Example 4:
Input: s = "LVIII"
Output: 58
Example 5:
Input: s = "MCMXCIV"
Output: 1994
Constraints:
PROGRAM CODE:
#include <iostream>
#include <string>
using namespace std;
int value(char r)
{
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
if (r == 'D')
return 500;
if (r == 'M')
return 1000;
return -1;
}
int res = 0;
int s1 = value(str[i]);
SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155
if (i + 1 < str.length())
{
int main()
{
system("pause 0");
return 0;
}
OUTPUT:
Write a function to find the longest common prefix string among an array of strings. If
there is no common prefix, return an empty string "".
Example 1:
Output: "fl"
Output: ""
Constraints:
PROGRAM CODE:
#include<iostream>
#include<algorithm>
if (n == 0)
return "";
if (n == 1)
return ar[0];
sort(ar, ar + n);
int en = min(ar[0].size(),
ar[n - 1].size());
int main()
{
int n{ 0 };
cout << "Enter the number of word. ";
cin >> n;
cout << "The longest common prefix is: " << longestCommonPrefix(strs, n) << endl;
return 0;
}
N/A
You will be given a list X of length L. You need to find whether there exists an element
which satisfies the following condition. Find y, such that
If there exist any y which satisfied this condition then print YES, otherwise print NO
Output will be
YES
Input
The first line contains T, the number of test cases. For each test case, the first line
contains L, the number of elements in the list X. The second line for each test case
contains L space-separated integers, denoting the list X.
Output
For each test case print YES if condition exists; otherwise print NO.
Constraints
1≤T≤10
1≤L≤10^5
1≤Xi ≤2×10^4
1≤i≤L
SAMPLE INPUT
123
NO
PROGRAM CODE:
#include<iostream>
int main()
int t;
cin >> t;
while (t--)
int n;
cin >> n;
arr[0] = 0;
sum += arr[i];
bool ok = true;
ok = false;
continue;
if (ok)
OUTPUT:
Example 1:
Input: n = 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
Input: n = 0
Output: 0
Example 3:
Input: n = 1
Output: 0
Constraints:
PROGRAM CODE:
#include <iostream>
using namespace std;
return true;
}
// Function to print number of primes
int printNumberPrime(int n)
{
int count{};
for (int i = 2; i <= n; i++) {
if (isPrime(i))
SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155
count++;
}
return count;
}
// Driver Code
int main()
{
int n{};
cin >> n;
cout << printNumberPrime(n);
return 0;
}
N/A
OUTPUT:
Starting with any positive integer, replace the number by the sum of the squares of its
digits.
Repeat the process until the number equals 1 (where it will stay), or it loops endlessly
in a cycle which does not include 1.
Example 1:
Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155
12 + 02 + 02 = 1
Example 2:
Input: n = 2
Output: false
PROGRAM CODE:
#include <iostream>
using namespace std;
return false;
}
// Driver code
int main()
{
int n = 23;
cin >> n;
if (isHappy(n))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
N/A
OUTPUT: