Found 10783 Articles for Python

Find largest subtree having identical left and right subtrees in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:52:45

80 Views

Suppose we have a binary tree; we have to find the largest subtree having identical left and right subtree. Preferred time complexity is O(n).So, if the input is likethen the output will beTo solve this, we will follow these steps −Define a function solve() . This will take root, encode, maxSize, maxNodeif root is None, thenreturn 0left_list := list with a blank stringright_list := list with a blank stringls := solve(root.left, left_list, maxSize, maxNode)rs := solve(root.right, right_list, maxSize, maxNode)size := ls + rs + 1if left_list[0] is same as right_list[0], thenif size > maxSize[0], thenmaxSize[0] := sizemaxNode[0] := rootencode[0] := ... Read More

Find k-th character of decrypted string - Set – 2 in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:49:33

361 Views

Suppose we have one encoded string where repetitions of substrings are represented as substring followed by count of substrings. As an example, if the string is "pq2rs2" and k=5, so output will be 'r', this is because the decrypted string is "pqpqrsrs" and 5th character is 'r'. We have to keep in mind that the frequency of encrypted substring can be of more than one digit.So, if the input is like string = "pq4r2ts3" and k = 11, then the output will be i, as the string is pqpqpqpqrrtststsTo solve this, we will follow these steps −encoded := blank stringoccurrence ... Read More

Find Indexs of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array - Set-2 in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:45:38

238 Views

Suppose we have one binary array. We have to find the position of 0 that can be replaced with 1 to get maximum number of continuous sequence of 1s.So, if the input is like [1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1], then the output will be 10, so the array will be [1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1].To solve this, we will follow these steps −i := 0, n := size of Acount_left := 0, count_right := 0max_i := -1, last_i := -1count_max := 0while i < ... Read More

Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:43:26

121 Views

Suppose we have two strings S1 and S2 of same lengths, we have to find an index i such that S1[0…i] and S2[i+1…n-1] give a palindrome when they are concatenated together. When it is not possible, return -1.So, if the input is like S1 = "pqrsu", S2 = "wxyqp", then the output will be 1 as S1[0..1] = "pq", S2[2..n-1] = "ypq", then S1 + S2 = "pqyqp" indicates is a palindrome.To solve this, we will follow these steps −n := size of str1str := blank stringfor i in range 0 to n, dostr := str concatenate str1[i]temp := blank ... Read More

Find if there is a path of more than k length from a source in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:41:27

137 Views

Suppose we have a graph, we also have a source vertex and a number k. The k is the path length of graph between source to destination, we have to check whether we can find a simple path (without cycle) starting from source and ending at any other vertex (as destination). The graph is shown in following −So, if the input is like Source = 0, k = 64, then the output will be True as there exists a simple path 0 to 7 to 1 to 2 to 8 to 6 to 5 to 3 to 4, this path ... Read More

Find if neat arrangement of cups and shelves can be made in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:38:41

86 Views

Suppose we have three different types of cups in array p and saucers in array q and m number of shelves, we have to check whether a neat arrangement of cups and shelves can be made.We can say that arrangement of the cups and saucers will be neat if it follows these conditions − 1. No shelf can hold both cups and saucers. 2. A self may contain at most 5 cups. 3. A self may contain at most 10 saucers.So, if the input is like p = [4, 3, 7] q = [5, 9, 10] m = 11, then ... Read More

Find if it is possible to get a ratio from given ranges of costs and quantities in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:36:21

46 Views

Suppose we have a range of cost from lowCost to upCost and another range of quantity from lowQuant to upQuant, we have to check whether we can find the given ratio r where r=cost/quantity, and lowCost ⇐ cost ⇐ upCost and lowQuant ⇐ quantity ⇐ upQuant.So, if the input is like lowCost = 2, upCost = 10, lowQuant = 3, upQuant = 9 and r = 3, then the output will be True as the cost = r * quantity = 3 * 3 = 9 where cost is in range [1, 10] and quantity is in [2, 8]To solve ... Read More

Find if given vertical level of binary tree is sorted or not in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:33:34

39 Views

Suppose we have binary tree; we have to check whether the given vertical level of the binary tree is sorted or not. When two nodes are overlapping, check they are in sorted sequence in the level they belong.So, if the input is like l = -1then the output will be True, as elements in level -1 are 3, 7 and they are sorted.To solve this, we will follow these steps −if root is null, thenreturn Trueprevious_value := -infcurrent_level := 0current_node := a new tree node with value 0Define one dequeue named qinsert (root, 0) at the end of qwhile q ... Read More

Find if an undirected graph contains an independent set of a given size in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:30:58

221 Views

Suppose we have a given undirected graph; we have to check whether it contains an independent set of size l. If there is any independent set of size l then return Yes otherwise No.We have to keep in mind that an independent set in a graph is defined as a set of vertices which are not directly connected to each other.So, if the input is like L = 4, then the output will be yesTo solve this, we will follow these steps −Define a function is_valid() . This will take graph, arrfor i in range 0 to size of arr, ... Read More

Find Four points such that they form a square whose sides are parallel to x and y axes in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:27:11

377 Views

Suppose we have n pair of points; we have to find four points so that they can generate a square whose sides are parallel to x and y axes otherwise return "not possible" If we can find more than one square then select the one with whose area is maximum.So, if the input is like n = 6, points = [(2, 2), (5, 5), (4, 5), (5, 4), (2, 5), (5, 2)], then the output will be 3, points are (2, 2) (5, 2) (2, 5) (5, 5)To solve this, we will follow these steps −my_map := a new mapfor ... Read More

Advertisements