Found 26504 Articles for Server Side Programming

Program to find Inorder Successor of a binary search tree in C++

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

701 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

Program to find number of 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

Program to 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

Program to find number of friend groups in a set of friends connections 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

Program to find maximum sum by flipping each row elements in Python

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

279 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

How to find the union of three vectors in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:14:01

920 Views

The union function in base R helps us to find the union of two vectors but if we have three vectors then the union cannot be directly created. For this purpose, we need to use union function twice. For example, if we have three vectors defined as x, y, and z then the union of these vectors can be found by using the command union(x, union(y, z)).Example1Live Demo> x1 y1 z1 union(x1, union(y1, z1))Output[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Example2Live Demo> x2 x2Output[1] 13 6 16 11 9 11 3 15 ... Read More

How to display the curve on the 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

How to create a bar chart using plotly in R?

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

406 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

How to create a covariance matrix in R?

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

672 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

Why scale_fill_manual does not fill the bar with colors created by using ggplot2 in R?

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

3K+ Views

We can manually fill the color of bars in a bar plot which is created by using ggplot2 but for this we would be needing color aesthetics already used for the bars. That means we need to use filling of bars inside aes of ggplot2 and then the colors can be mentioned inside the scale_fill_manual function.ExampleConsider the below data frame:Live Demo> x count df dfOutputx count 1 A 321 2 B 324 3 C 320 4 D 328Loading ggplot2 package and creating bar plot:Example> library(ggplot2) > ggplot(df, aes(x, count))+geom_bar(stat="identity")Output:Creating the bar plot by using scale_fill_manual:Example> ggplot(df, aes(x, count))+geom_bar(stat="identity")+scale_fill_manual(values=c("blue", "green", "grey", ... Read More

Advertisements