We are given a perimeter P of a triangle. Perimeter is the sum of all sides of the triangle. The goal is to find the number of right triangles that can be made which have the same perimeter.If the sides of the triangle are a, b and c. Then a + b + c = P and a2 + b2 = c2 ( pythagoras theorem for any combination of a, b, and c )We will check this by taking a from 1 to p/2 and b from a+1 to p/3. Then c = p-a-b (a+b+c=p)For all right triangles, apply Pythagoras ... Read More
We are given with a rectangle of length L and breadth B, such that L>=B. The goal is to find the number of squares that a rectangle of size LXB can accommodate.Above figure shows a rectangle of size 3 X 2. It has 2, 2X2 squares and 6, 1X1 squares in it.Total squares= 6+2=8.Every rectangle of size LXB has L*B number of 1X1 squares.Biggest squares are of size BXB.For L=B=1, squares = 1.For L=B=2, squares = 1 + 4 = 5. ( 1 of 2X2, 4 of 1X1 )For L=B=3, squares = 1 + 4 + 9 = 14. ( ... Read More
We are given an array of integers of size N. Variables L and R define a range between 1 and N. The goal is to find the number of smallest elements that lie in range L and R such that L>=1 and R
We are given with an array of numbers. The goal is to find the count of prime numbers in that array.A prime number is the one which is divisible by 1 and the number itself. It has only two factors. We will check if the number is prime starting from the first element till the last and increase the count of prime numbers found so far.To check if the number N is prime, check if numbers between the range [2 to N/2], fully divides N. If yes then it is non-prime. Else it is prime.Let’s understand with examples.Input − arr[]= ... Read More
We are given with five integers N, A, B, X and Y. The goal is to maximize the profit by checking that between numbers in range [ 1 to N ] , ifA number is divisible by A, then profit increases by X.A number is divisible by B then profit increases by Y.A profit can be added once only, for a particular number in range.Let’s understand with examples.Input − N=4, A=2, B=3, X=2, Y=3Output − Maximized profit is − 7Explanation −2, 4 are divisible by A ( 2 ). Profit increases from 0 to 2, then 2 to 4 ( ... Read More
We are given with an array of N numbers with elements lying in range 0 and N-1. The elements are unsorted. The goal is to find the maximum number of partitions of the array which can be sorted individually and then can be concatenated to make a whole sorted array of length N.Each partition is chosen such that elements in it are unsorted. For N numbers ranging between 0 and N-1, sorted elements are at index equal to the value. Arr[i] = i.We will solve this by comparing each element by maximum value found so far on its left. When ... Read More
There are multiple ways to save a plot created in R. Base R provides, metafile, bitmap, and postscript options to copy and save the plots created in R but we can also save the plots created with ggplot2 as an SVG file with the help of svglite package. The ggsave function of svglite package does this job easily and we can also define the height and width of the plot inside this function.Example Live DemoInstall the svglite package −install.packages("svglite")Consider the ToothGrowth data and create a scatterplot between len and dose −head(ToothGrowth) len supp dose 1 4.2 VC ... Read More
The reason we get newdata had 1 row warning is the newdata is not correctly defined. We should give the name of the explanatory variable or independent variable to the newdata so that the model can identify that we are passing the mean of the explanatory variable, otherwise it considers all the values of the explanatory hence the result of the predict function yields the predicted values for the sample size.Example Live DemoConsider the below data frame −set.seed(123) x
When a bar plot is created then the distance or space between bars is equal but sometimes the width of the bar is large, therefore, it becomes a little difficult to understand the difference between those bars especially in cases when the data values are not very much different from each other. To overcome this visualization problem, we can create a bar plot with some space between the bars and it can be done with the help of width argument of geom_bar in ggplot2.ExampleConsider the below data frame −x
Most of the times, the string data is in bad shape and we need to make it appropriate so that we can easily proceed with the analysis. There is also a situation in which a string column has some values where an extra space is used which was not required, therefore, it does not match with the rest of the column values. To remove these spaces, we can use lapply and gsub function.ExampleConsider the below data frame −x1