Suppose we have a number n, we have to find the number of strings of length n can be generated using the following rules −Each character is a lower case vowel [a, e, i, o, u]"a" may only be followed by one "e""e" may only be followed by any of "a" and "i""i" may not be followed by another "i""o" may only be followed by any of "i" and "u""u" may only be followed by one "a"If the result is very large, mod the result by 10^9 + 7.So, if the input is like n = 2, then the output ... Read More
The R-squared value is the coefficient of determination, it gives us the percentage or proportion of variation in dependent variable explained by the independent variable. To display this value on the scatterplot with regression model line without taking help from any package, we can use plot function with abline and legend functions.Consider the below data frame −Example Live Demoset.seed(1234) x
Suppose we have a list of strings called genes where each element has the same length and each element contains characters "A", "C", "G" and/or "T". Now there are some rules −When two strings s1 and s2 are the same string except for one character, then s1 and s2 are in the same mutation group.When two strings s1 and s2 are in a group and s2 and s3 are in a group, then s1 and s3 are in the same group.We have to find the total number of mutation groups we can generate.So, if the input is like genes = ... Read More
If a vector contains five values then there will be ten pairwise differences. For example, suppose we have five numbers starting from 1, then the pairwise combinations for these values will be (1,2), (1,3), (1,4), (1,5), (2,3), (2,4), (2,5), (3,4), (3,5), (4,5). Now to find the absolute pairwise differences, we would be need to find the differences between each of these combinations and take the absolute value of the answer hence the result will be 1, 2, 3, 4, 1, 2, 3, 1, 2, 1.Example Live Demox1
The best way to create a sequence of anything is creating it with the help of seq function and this also applies to sequences of dates. But in case of dates, we need to read the dates in date format so that R can understand the input type and create the appropriate vector. If we do not use the date format for the date value then it won’t make sense to R and it will result in error.Examples Live Demox1
Suppose we have two lists, weights and values of same length and another value capacity. The weights[i] and values[i] represent the weight and value of ith element. So if we can take at most capacity weights, and that we can take a fraction of an item's weight with proportionate value, we have to find the maximum amount of value we can get (rounded down to the nearest integer)So, if the input is like weights = [6, 7, 3] values = [110, 120, 2] capacity = 10, then the output will be 178.To solve this, we will follow these steps −res ... Read More
The sum of rows and columns on the margins in a contingency table are always useful because they are used for different type of calculations such as odds ratio, probability etc. If an R data frame has factor columns then we can create a contingency table for that data frame and it can be done by using addmargins function.ExampleConsider the below data frame − Live Demox1
Suppose we have a graph, represented as a list of edges. We have to check whether the graph is a collection of trees (forest) or not.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps −Define a function dfs() . This will take node, previf node in seen, thenreturn Falseinsert node into seenfor each adjacent node n in e[node], doif n is not same as prev, thenif dfs(n, node) is false, thenreturn Falsereturn TrueFrom the main method, do the following −e := an empty mapfor each start node u and end node ... Read More
Suppose we have a list of airline tickets represented by pairs of departure and arrival airports like [from, to], we have to reconstruct the itinerary in correct order. All of the tickets belong to a man who departs from KLK. So, the itinerary must begin with JFK.So if the input is like [["MUC", "LHR"], ["KLK ", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]], then the output will be ["KLK ", "MUC", "LHR", "SFO", "SJC"].To solve this, we will follow these steps −Define array ret and a map called graph.Define a method called visit. This will take airport name as inputwhile size of ... Read More
Suppose we have a list of sorted list of distinct integers of size n, we have to find the first positive number in range [1 to n+1] that is not present in the array.So, if the input is like nums = [0, 5, 1], then the output will be 2, as 2 is the first missing number in range 1 to 5.To solve this, we will follow these steps −target := 1for each i in arr, doif i is same as target, thentarget := target + 1return targetLet us see the following implementation to get better understanding −Example Live Democlass Solution: ... Read More