Suppose we have a list of words, here each word can be written as a concatenation of the Morse code of each letter. For example, the word "cba" can be written as "-.-..--...", this is the concatenation "-.-." | "-..." | ".-"). This kind of concatenation is called the transformation of a word.We know that International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on.Here is the list of all 26 letters of the ... Read More
Following is the Java program to find the vertex, focus and directrix of a parabola −Example Live Demopublic class Demo{ public static void find_values(float val_1, float val_2, float val_3){ System.out.println("The value of vertex is (" + (-val_2 / (2 * val_1)) + ", "+ (((4 * val_1 * val_3) - (val_2 * val_2)) / (4 * val_1)) + ")"); System.out.println("The value of focus is (" + (-val_2 / (2 * val_1)) + ", " + (((4 * val_1 * val_3) - (val_2 * val_2) + 1) / (4 * val_1)) + ")"); ... Read More
Suppose we have two integers L and R, we have to find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary form.So, if the input is like L = 6 and R = 10, then the output will be 4, as there are 4 numbers 6(110), 7(111), 9(1001), 10(1010), all have prime number of set bits.To solve this, we will follow these steps −count := 0for j in range L to R, doif set bit count of j is in [2, 3, 5, 7, 11, 13, 17, 19], thencount ... Read More
Following is the Java code to find the perimeter of a cylinder −Example Live Demoimport java.io.*; public class Demo{ static int find_peri(int dia, int ht){ return 2*(dia + ht); } public static void main(String[] args){ int dia = 7; int ht = 15; System.out.println("The perimeter of the cylinder is " + find_peri(dia, ht) + " units"); } }OutputThe perimeter of the cylinder is 44 unitsA class named Demo defines a static function that takes in two values, diameter and height. This function computes the sum ... Read More
Suppose we have a dictionary words, and we have to find the minimum length word from a given dictionary words, it has all the letters from the string licensePlate. Now such a word is said to complete the given string licensePlate. Here, we will ignore case for letters. And it is guaranteed an answer exists. If there are more than one answers, then return answer that occurs first in the array.The license plate there may be same letter occurring multiple times. So when a licensePlate of "PP", the word "pile" does not complete the licensePlate, but the word "topper" does.So, ... Read More
Following is the Java code to find the largest prime factor of a number −Example Live Demoimport java.io.*; import java.util.*; public class Demo{ static long maxPrimeFactors( long val){ long max_prime = -1; while (val % 2 == 0) { max_prime = 2; val >>= 1; } for (int i = 3; i 2) max_prime = val; return max_prime; } public static void main(String[] args){ int val = 148592; ... Read More
Suppose we have an integer array called nums, now there is always exactly one largest element. We have to check whether the largest element in the array is at least twice as much as every other number in the array. If it is so, then we have to find the index of the largest element, otherwise return -1.So, if the input is like [3, 6, 1, 0], then the output will be 1, as 6 is the largest number, and for every other number in the array x, 6 is more than twice as big as x. As the index ... Read More
Followimg is the code to implement String formatting in Java using % −Example Live Demopublic class Demo { public static void main(String args[]){ String my_str = " sample."; String concat_Str = String.format("This is a" + "%s", my_str); String format_str_1 = String.format("The value is %.4f", 78.92367); System.out.println(concat_Str); System.out.println(format_str_1); } }OutputThis is a sample. The value is 78.9237A class named Demo contains the main function. Here a string value is defined, which is used to format the string, by concatenating it to another variable. Similarly, a ... Read More
Suppose there is a staircase, here the i-th step will be some non-negative cost value cost[i] assigned. When we pay the cost, we can either climb one or two steps. We have to find minimum cost to reach the top of the floor, and we also can either start from the step with index 0, or the step with index 1.So, if the input is like cost = [12, 17, 20], then the output will be 17, The cheapest position to start from step 1 as we have to pay that cost and go to the top.To solve this, we ... Read More
Suppose we have a list of sorted characters’ letters. This is containing only lowercase letters, now we have a target letter t, we have to find the smallest element in the list that is larger than the given target.And letters also wrap around. So, if the target is t = 'z' and letters = ['a', 'b'], the answer is 'a'.So, if the input is like ["c", "f", "j"], t = 'a', then the output will be 'c'.To solve this, we will follow these steps −l := 0r := size of letters - 1while l target, thenr := mid -1otherwise, ... Read More