Find Total Unique Duration from a List of Intervals in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:25:00

1K+ Views

Suppose we have a list of intervals where each list represents an interval [start, end] (inclusive). We have to find the total unique duration it covers.So, if the input is like intervals = [[2, 11], [13, 31], [41, 61]], then the output will be 50, as the total unique covered distance is (11 - 2 + 1) = 10 then (31 - 13 + 1) = 19 and (61 - 41 + 1) = 21, so total is 50.To solve this, we will follow these steps −if intervals list is empty, thenreturn 0sort the list intervals[start, end] := intervals[0]ans := ... Read More

Find Intervals That Do Not Intersect the Cut Interval in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:23:27

235 Views

Suppose we have a sorted and disjoint intervals list and another list cut, that represents an interval. We have to delete all parts of intervals that are intersecting with cut interval, and return the new list.So, if the input is like intervals = [[2, 11], [13, 31], [41, 61]] cut = [8, 46], then the output will be [[2, 8], [46, 61]]To solve this, we will follow these steps −cut_start, cut_end := cutans := a new listfor each start, end in intervals, doif maximum of cut_start and start < minimum of end and cut_end, thenif start < cut_start, theninsert interval ... Read More

Find Inorder Successor of a Binary Search Tree in C++

Arnab Chakraborty
Updated on 19-Nov-2020 06:21:30

700 Views

Suppose we have a binary search tree BST and another value of a node, we have to find the in-order successor of that node in the BST. As we all know that the successor of a node p is the node with the smallest key greater than the value of p.So, if the input is likeAnd p = 1, then the output will be 2, To solve this, we will follow these steps −Define recursive method inorderSuccessor(), this will take root and pif root null, then:return nullif val of root val val){             return inorderSuccessor(root->right, ... Read More

Find Minimum Steps to Reach Last Index in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:19:24

533 Views

Suppose we have a list of numbers called nums and we are placed currently at nums[0]. On each step, we can either jump from the current index i to i + 1 or i - 1 or j where nums[i] == nums[j]. We have to find the minimum number of steps required to reach the final index.So, if the input is like nums = [4, 8, 8, 5, 4, 6, 5], then the output will be 3, as we can jump from index 0 to index 4 as their values are both 4. And then we jump back to index ... Read More

Find H-Index from a List of Citations in C++

Arnab Chakraborty
Updated on 19-Nov-2020 06:17:15

350 Views

Suppose we have an array of citations of a researcher. We have to define a function to compute the researcher's h-index. As we know the h-index is a metric used to calculate the impact of a researcher's papers. Formally H-index can be defined as: "A researcher has index h if h of their N papers have at least h citations each, and the other N − h papers have no more than h citations each."So, if the input is like citations = [5, 4, 1, 2, 6], then the output will be 3, as at least 3 papers have at ... Read More

Find Number of Friend Groups in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:15:35

790 Views

Suppose we have a a friends list, where friends[i] is a list of people i is friends with. The connection of friendships are two-way. And each person is friend with themselves and two people are in a friend group as long as there is some path of mutual friends connecting them. We have to find the total number of friend groups.So, if the input is like friends = [[0, 1, 5], [1, 0], [2], [3, 4], [4, 3], [5, 0]], then the output will be 3, as The three friend groups are as below −To solve this, we will follow ... Read More

Find Maximum Sum by Flipping Each Row Elements in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:13:02

278 Views

Suppose we have a 2D binary matrix. For any row or column in the given matrix we can flip all the bits. If we can perform any number of these operations, and that we treat each row as a binary number, we have to find the largest sum that can be made of these numbers.So, if the input is like010001then the output will be 11, as if we flip both rows we get 101 and 110, then the sum is 5 + 6 = 11To solve this, we will follow these steps −for each row r in matrix, doif r[0] ... Read More

Display Curve on Histogram Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 19-Nov-2020 05:21:30

3K+ Views

Mostly, we use histogram to understand the distribution of a variable but if we have an overlay line on the histogram that will make the chart smoother, thus understanding the variation will become easy. To display the curve on the histogram using ggplot2, we can make use of geom_density function in which the counts will be multiplied with the binwidth of the histogram so that the density line will be appropriately created.ExampleConsider the below data frame:Live Demo> x df head(df, 20)Output x 1 4 2 5 3 6 4 4 5 9 6 ... Read More

Create Bar Chart Using Plotly in R

Nizamuddin Siddiqui
Updated on 19-Nov-2020 05:19:13

403 Views

Plotly in R is a package specifically designed to create highly-interactive and publication-quality charts. The chart can be created by using plot_ly function of the package and there are three main arguments of plot_ly defined as x, y, and type, where x refers to the X-axis, y refers to the Y-axis and type refers to the chart type but the axes values are stored in a data frame or itself a shared.ExampleLoading plotly package:> library(plotly)Consider the below data frame:Live Demo> x count df dfOutputx count 1 A 321 2 B 324 3 C 320 4 D 328Creating the bar plot ... Read More

Create a Covariance Matrix in R

Nizamuddin Siddiqui
Updated on 19-Nov-2020 05:15:11

670 Views

To create a covariance matrix, we first need to find the correlation matrix and a vector of standard deviations is also required. The correlation matrix can be found by using cor function with matrix object. For example, if we have matrix M then the correlation matrix can be found as cor(M). Now we can use this matrix to find the covariance matrix but we should make sure that we have the vector of standard deviations.Example1Live Demo> M1 M1Output [, 1] [, 2] [, 3] ... Read More

Advertisements