Python Articles

Page 539 of 852

Program to find out the lowest common ancestor of a binary tree using parent pointers using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-May-2021 232 Views

Suppose, we are given a binary tree and also two specific nodes x and y. We have to find out the lowest common ancestor of the two nodes from the binary tree. The lowest common ancestor in a binary tree is the lowest node of which both the nodes x and y are descendants. A particular node can also be a descendant of itself. We have to find the node and return it as an output.The node structure of the tree is like below −TreeNode:    data:    left:    right:    parent: We have to utilize ...

Read More

Program to fix a erroneous binary tree using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-May-2021 272 Views

Suppose, we are given a binary tree that has a problem; one of the node's right child pointer points to another node at the same level in the binary tree erroneously. So, to fix this problem, we have to find out the node that has this error and delete that node and its descendants except the node that it is erroneously pointing to. We return the root node of the fixed binary tree.So, if the input is likeWe can see that there is an erroneous link between 4 and 6. The right child pointer of 4 points to 6.then the ...

Read More

Program to change the root of a binary tree using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-May-2021 968 Views

Suppose, we are given a binary tree and a node that is situated at the leaf of the binary tree.We have to make the leaf node the root node of the binary tree. We can do it in the following way −If a node has a left child, it becomes the right child.A node's parent becomes its left child. In this process, the parent node's link to that node becomes null, so it will have only one child.The node structure of the tree is like below −TreeNode:    data:    left:    right:    parent: We have ...

Read More

Program to count number of square submatrices with all ones using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-May-2021 242 Views

Suppose we have m x n binary matrix, we have to find how many square submatrices have all ones.So, if the input is like.011111110111then the output will be 15, as there are 10 squares of side 1. 4 squares of side 2 and 1 square of side 3. Then total number of squares = 10 + 4 + 1 = 15.To solve this, we will follow these steps −if matrix has single one, thenreturn 1rows := row count of matrixcols := column count of matrix[0]result := 0for row in range 0 to rows - 1, dofor col in range 0 ...

Read More

Program to count submatrices with all ones using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-May-2021 691 Views

Suppose we have m x n binary matrix, we have to find how many submatrices have all ones.So, if the input is like101011011then the output will be 13 as there are 6 (1x1) matrices, 3 (2, 1) matrices, 2 (1x2) matrices, 1 (3x1) matrix and 1 (4x4) matrix.To solve this, we will follow these steps −m := row count of matrixn := column count of matrixdp := a zero matrix of same size m x nfor i in range 0 to m - 1, dofor j in range 0 to n - 1, doif i is same as 0 and ...

Read More

Program to find range sum of sorted subarray sums using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-May-2021 441 Views

Suppose we have an array nums with n positive elements. If we compute the sum of all non-empty contiguous subarrays of nums and then sort them in non-decreasing fashion, by creating a new array of n*(n+1)/2 numbers. We have to find the sum of the numbers from index left to index right (1-indexed), inclusive, in the new array. The answer may be very large so return result modulo 10^9 + 7.So, if the input is like nums = [1, 5, 2, 6] left = 1 right = 5, then the output will be 20 because here all subarray sums are ...

Read More

Program to find minimum difference between largest and smallest value in three moves using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-May-2021 685 Views

Suppose we have an array called nums. We can change one element from this array to any value in one move. We have to find the minimum difference between the largest and smallest value of nums after preforming at most 3 moves.So, if the input is like nums = [3,7,2,12,16], then the output will be 1 because we can make given array to [1,1,0,1,1], so the maximum is 1 and minimum is 0, so the difference is 1.To solve this, we will follow these steps −if size of nums

Read More

Program to find path with maximum probability using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-May-2021 567 Views

Suppose we have an undirected weighted graph with n nodes (nodes are numbered from 0 onwards), This graph is given as input using edge list, for each edge e, it has a probability of success of traversing that edge probability[e]. We also have start and end nodes, we have to find the path with the maximum probability of success to go from start to end and return its success probability. If we cannot find any path, then return 0.So, if the input is likethen the output will be 0.24 because there are two paths from node 0 to 2, one ...

Read More

Program to find number of nodes in the sub-tree with the same label using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-May-2021 471 Views

Suppose we have a rooted general tree with n nodes, whose nodes are numbered from 0 to n-1. Each node has a label with lowercase English letter. The labels are given as input in labels array, where lables[i] contains label for ith node. The tree is represented by edge list where each edge e has [u, v] represents u is parent and v is child. We have to find an array A of size n, represents number of nodes in the subtree of the ith node with same label as iSo, if the input is likeHere n = 5 and ...

Read More

Program to find number of good ways to split a string using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-May-2021 402 Views

Suppose we have a string s. Now a split is said to be a good split when we can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the equal. We have to find the number of good splits we can make in s.So, if the input is like s = "xxzxyx", then the output will be 2 as there are multiple ways of splitting but if we split like ("xxz", "xyx") or ("xxzx", "yx") then they are good.To solve this, we ...

Read More
Showing 5381–5390 of 8,519 articles
« Prev 1 537 538 539 540 541 852 Next »
Advertisements