Block SearchJust like Binary Search, Block Search is also a searching algorithm for sorted arrays. The basic idea is to check fewer elements (than linear search) by jumping ahead by fixed steps or skipping some elements in place of searching all elements.For exampleSuppose we have an array arr of length n and block (to be jumped) of size m. Then we search at the indexes arr[0], arr[m], arr[2 * m], ..., arr[k * m] and so on.Once we find the interval arr[k * m] < x < arr[(k+1) * m], we perform a linear search operation from the index k ... Read More
Hamming DistanceThe Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different.For example, consider the following strings −const str1 = 'delhi'; const str2 = 'delph';The Hamming Distance of these strings is 2 because the fourth and the fifth characters of the strings are different. And obviously in order to calculate the Hamming Distance we need to have two strings of equal lengths.Therefore, we are required to write a JavaScript function that takes in two strings, let’s say str1 and str2, and returns their hamming distance.ExampleFollowing is the code −const str1 ... Read More
The least common multiple, (LCM) of two integers a and b, is the smallest positive integer that is divisible by both a and b.For example −LCM of 4 and 6 is 12 because 12 is the smallest number that is exactly divisible by both 4 and 6.We are required to write a JavaScript function that takes in two numbers, computes and returns the LCM of those numbers.ExampleFollowing is the code −const num1 = 4; const num2 = 6; const findLCM = (num1, num2) => { let hcf; for (let i = 1; i
Suppose we are given a set of candidate numbers (without duplicates) and a target number (target).We are required to write a function that finds all unique combinations in candidates where the candidate numbers sum to the target.The same repeated number may be chosen from candidates an unlimited number of times.Note −All numbers (including target) will be positive integers.The solution set must not contain duplicate combinations.For example −If the inputs are −candidates = [2, 3, 6, 7], target = 7, The solution to this can be −[ [7], [2, 2, 3] ];Since the problem is to get all the ... Read More
Suppose we are given an input string str and a pattern p, we are required to implement regular expression matching with support for. and *.The functions of these symbols should be −. --> Matches any single character.* --> Matches zero or more of the preceding elements.The matching should cover the entire input string (not partial).Notestr could be empty and contains only lowercase letters a-z.p could be empty and contains only lowercase letters a-z, and characters like. or *.For example −If the input is −const str = 'aa'; const p = 'a';Then the output should be false because a does not ... Read More
We are required to write a JavaScript function that takes in a number and determines whether or not it is a power of two.For example −f(23) = false f(16) = true f(1) = true f(1024) = trueApproach −Powers of two in binary form always have just one bit. Like this −1: 0001 2: 0010 4: 0100 8: 1000Therefore, after checking that the number is greater than zero, we can use a bitwise hack to test that one and only one bit is set. The same is shown below −num & (num - 1)ExampleFollowing is the code −const num1 = 256; ... Read More
In mathematics, Euclid's algorithm, is a method for computing the greatest common divisor (GCD) of two numbers, the largest number that divides both of them without leaving a remainder.The Euclidean algorithm is based on the principle that the greatest common divisor of two numbers does not change if the larger number is replaced by its difference with the smaller number.For example, 21 is the GCD of 252 and 105 (as 252 = 21 × 12 and 105 = 21 × 5), and the same number 21 is also the GCD of 105 and 252 − 105 = 147.Since this replacement ... Read More
A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. All other natural numbers greater than 1 are called composite numbers. A primality test is an algorithm for determining whether an input number is prime.We are required to write a JavaScript function that takes in a number and checks whether it is a prime or not.ExampleFollowing is the code −const findPrime = (num = 2) => { if (num % 1 !== 0) { return false; } if (num
To check for such conditions, use IF() in MySQL.Let us create a table −Examplemysql> create table demo89 -> ( -> duedate date -> ); Query OK, 0 rows affected (0.78Insert some records into the table with the help of insert command −Examplemysql> insert into demo89 values('2020-01-10'); Query OK, 1 row affected (0.55 mysql> insert into demo89 values(null); Query OK, 1 row affected (0.13 mysql> insert into demo89 values('2020-11-29'); Query OK, 1 row affected (0.15 mysql> insert into demo89 values('2019-11-29'); Query OK, 1 row affected (0.09Display records from the table using select statement −Examplemysql> select ... Read More
To create a table, you need to insert below line into application.properties −spring.jpa.hibernate.ddl-auto=updateHere, Hibernate will create the table demo88 automatically. The application.properties code is as follows −spring.datasource.platform=mysql spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate.ddl-auto=update server.port=8191 spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/sampledatabase spring.datasource.username=root spring.datasource.password=123456The demo88 entity class is as follows to create table columns −Examplepackage com.automaticallytablecreation; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table public class demo88 { @Id private int id; @Column(name="name") private String name; }The main class code is as follows −Examplepackage com.automaticallytablecreation; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class AutomaticTableApplication { public static void main(String[] ... Read More