Suppose we have a binary tree; we have to find the longest path between any two nodes in the tree.So, if the input is like then the output will be 5 To solve this, we will follow these steps:ans := 0Define a function getMaxPath() . This will take nodeif node is null, thenreturn 0leftCnt := getMaxPath(left of node)rightCnt := getMaxPath(right of node)temp := 1 + maximum of leftCnt and rightCntans := maximum of ans and l+r+1From the main method do the following −getMaxPath(root)return ansLet us see the following implementation to get better understanding −Example Live Democlass TreeNode: def __init__(self, val, left=None, right=None): ... Read More
Suppose we have a string s, we have to find the length of the longest substring that contains at most 2 distinct characters.So, if the input is like s = "xyzzy", then the output will be 4, as "yzzy" is the longest substring with at most 2 unique characters.To solve this, we will follow these steps−start := 0c := a mapans := 0for end in range 0 to size of s, doc[s[end]] := c[s[end]] + 1while size of c > 2, doc[s[start]] := c[s[start]] - 1if c[s[start]] is 0, thendelete c[s[start]]start := start + 1ans := maximum of ans and ... Read More
Suppose we have a list of numbers called nums, we have to find the length of the longest sublist where 2 * minimum of sublist > maximum of sublist.So, if the input is like nums = [10, 2, 6, 6, 4, 4], then the output will be 4, as the sublist [6, 6, 4, 4] is the longest sublist that holds the criteria as 2 * 4 > 6.To solve this, we will follow these steps−ret := 0define two double ended queues minq and maxql := 0, r := 0while r < size of nums, don := nums[r]while minq and ... Read More
Suppose we have a string S. We have to find the length of longest palindromic substring in S. We are assuming that the length of the string S is 1000. So if the string is “BABAC”, then the longest palindromic substring is “BAB” and length is 3.To solve this, we will follow these steps −Define one square matrix of order same as the length of string, and fill it with FalseSet the major diagonal elements as true, so DP[i, i] = True for all i from 0 to order – 1start := 0for l in range 2 to length of ... Read More
Suppose we have a list of mumbers called nums, and find the length of the longest sublist in nums such that the equality relation between every consecutive numbers changes alternatively between less-than and greater-than operation. The first two numbers' inequality may be either less-than or greater-than.So, if the input is like nums = [1, 2, 6, 4, 5], then the output will be 4, as the longest inequality alternating sublist is [2, 6, 4, 5] as 2 < 6 > 4 < 5.To solve this, we will follow these steps −Define a function get_direction(). This will take a, breturn 0 ... Read More
There is no function in R to calculate the population variance but we can use the population size and sample variance to find it. We know that the divisor in population variance is the population size and if we multiply the output of var(it calculates sample variance) function with (population size – 1)/population size then the output will be population variance.Example Live Demoset.seed(141) x1
Generally, if we extract a single column from an R data frame then it is extracted as a vector but we might want it in data frame form so that we can apply operations of a data frame on it. Therefore, we can use single square brackets for the extraction with T (TRUE) or (FALSE) values and drop = FALSE so that the output becomes a data frame.Consider the below data frame −Example Live Demoset.seed(999) x1
If we have repeated values in a vector that means we have ties in the vector, therefore, the indexes of values will help us to identify the positions of a particular value in the vector. We can use which function with min function to find the positions of minimum values in a vector, if there exists more than one minimum then the output will show all the relevant positions.Example Live Demox1
To create predictive models, it is necessary to create three subsets of a data set for the purpose of training the model, testing the model and checking the validation of the model. These subsets are usually called train, test and validation. For this purpose, we can use different type of sampling methods and the most common is random sampling. In the below example, you can see how it can be done.Consider the mtcars data set in base R −Example Live Demodata(mtcars) str(mtcars)Output'data.frame':32 obs. of 11 variables: $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ... ... Read More
Suppose we have a list of numbers. We have to find the length of longest increasing subsequence. So if the input is like [6, 1, 7, 2, 8, 3, 4, 5], then the output will be 5, as the longest increasing subsequence is [2, 3, 4, 5, 6].To solve this, we will follow these steps −Make an array called tails whose size is same as nums, and fill this with 0.size := 0for each element x in nums array −i := 0, j := sizewhile i is not same as j, thenmid := i + (j – i)/2if tails[mid] < ... Read More