Questions are all easy don't get into words just think about logics
Whatever questions shared are only for daily practice if you will do then only think of good product base companies
Whatever questions shared are only for daily practice if you will do then only think of good product base companies
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
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:
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given an integer, convert it to a roman numeral.
Example 1:
Input: num = 3
Output: "III"
Explanation: 3 is represented as 3 ones.
Example 2:
Input: num = 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.
Example 3:
Input: num = 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Constraints:
1 <= num <= 3999
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
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:
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given an integer, convert it to a roman numeral.
Example 1:
Input: num = 3
Output: "III"
Explanation: 3 is represented as 3 ones.
Example 2:
Input: num = 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.
Example 3:
Input: num = 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Constraints:
1 <= num <= 3999
Same code with execution difference ๐๐ one is in top 1% another one is 30%
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, 2 is written as II in Roman numeral, just two ones 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:
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given a roman numeral, convert it to an integer.
Example 1:
Input: s = "III"
Output: 3
Explanation: III = 3.
Example 2:
Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.
Example 3:
Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Constraints:
1 <= s.length <= 15
s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
It is guaranteed that s is a valid roman numeral in the range [1, 3999].
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, 2 is written as II in Roman numeral, just two ones 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:
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given a roman numeral, convert it to an integer.
Example 1:
Input: s = "III"
Output: 3
Explanation: III = 3.
Example 2:
Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.
Example 3:
Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Constraints:
1 <= s.length <= 15
s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
It is guaranteed that s is a valid roman numeral in the range [1, 3999].
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Example 1:
Input: strs = ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Constraints:
1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] consists of only lowercase English letters.
If there is no common prefix, return an empty string "".
Example 1:
Input: strs = ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Constraints:
1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] consists of only lowercase English letters.
Java Hyd Team pinned ยซOne question for you all where is main method in these codes ?? And if not why notยป
One message for you all you will never see these codes with main method without main method you can't excute codes in normal IDE so these codes will only work on competitive coding platforms for normal IDE you can just copy logic n use methods n constructors
Java Hyd Team pinned ยซOne message for you all you will never see these codes with main method without main method you can't excute codes in normal IDE so these codes will only work on competitive coding platforms for normal IDE you can just copy logic n use methods n constructorsยป
// Thread synchronization
import java.util.*;
import java.lang.*;
public class Number{
static int x;
synchronized static void increment(){
x++;
}
}
class First extends Thread {
public void run(){
for(int i= 1 ; i<= 100000 ; i++){
Number.increment();
}
}
}
class Second extends Thread{
public void run(){
for(int i= 1 ; i<= 100000 ; i++){
Number.increment();
}
}
}
class Synchronization{
public static void main(String[] args) {
First f = new First();
Second s = new Second();
f.start ();
s.start();
try{
f.join();
s.join();
} catch (Exception e ){
}
System.out.println("Final x value " + Number.x);
}
import java.util.*;
import java.lang.*;
public class Number{
static int x;
synchronized static void increment(){
x++;
}
}
class First extends Thread {
public void run(){
for(int i= 1 ; i<= 100000 ; i++){
Number.increment();
}
}
}
class Second extends Thread{
public void run(){
for(int i= 1 ; i<= 100000 ; i++){
Number.increment();
}
}
}
class Synchronization{
public static void main(String[] args) {
First f = new First();
Second s = new Second();
f.start ();
s.start();
try{
f.join();
s.join();
} catch (Exception e ){
}
System.out.println("Final x value " + Number.x);
}
// creating threads using runnable interface
//runnable interface has only one specification called run()
// we can't start runnable object directly
// we can create the thread object by passing runnable object as parameter
// we can execute the thread logic by starting thread '
public class Main {
public static void main(String[] args) {
First f = new First ();
// f.start (); error
Thread t = new Thread (f);
t.start();
}
}
class First implements Runnable {
public void run(){
System.out.print("thread logic ");
}
}
//runnable interface has only one specification called run()
// we can't start runnable object directly
// we can create the thread object by passing runnable object as parameter
// we can execute the thread logic by starting thread '
public class Main {
public static void main(String[] args) {
First f = new First ();
// f.start (); error
Thread t = new Thread (f);
t.start();
}
}
class First implements Runnable {
public void run(){
System.out.print("thread logic ");
}
}
๐1
//Find sum of n numbers through threading ...
import java.util.*;
public class Defaults {
static int n ;
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
System.out.print("Sum of N numbers ");
System.out.print("Enter N number ");
n = x.nextInt();
Calculation Thread = new Calculation();
Thread.start();
try{
Thread.join();}
catch(Exception e ){}
System.out.print("Sum "+Calculation.sum);
}
}
class Calculation extends Thread{
static int sum =0;
public void run(){
System.out.print("Calc Started ");
for(int i = 1 ; i<= Defaults.n ; i++){
Calculation.sum= Calculation.sum+i;
}
System.out.print("Calc ends ");
}
}
import java.util.*;
public class Defaults {
static int n ;
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
System.out.print("Sum of N numbers ");
System.out.print("Enter N number ");
n = x.nextInt();
Calculation Thread = new Calculation();
Thread.start();
try{
Thread.join();}
catch(Exception e ){}
System.out.print("Sum "+Calculation.sum);
}
}
class Calculation extends Thread{
static int sum =0;
public void run(){
System.out.print("Calc Started ");
for(int i = 1 ; i<= Defaults.n ; i++){
Calculation.sum= Calculation.sum+i;
}
System.out.print("Calc ends ");
}
}