Find Length of Longest Path with Even Sum in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:39:35

208 Views

Suppose we have a binary tree. we have to find the length of the longest path whose sum is an even number.So, if the input is like image, then the output will be 5, as the path is like [5, 2, 4, 8, 5], sum = 24(even).To solve this, we will follow these steps −Define a function dfs() . This will take nodeif node is null, thenreturn a pair (0, -inf)(left_0, left_1) := dfs(left of node)(right_0, right_1) := dfs(right of node)if value of node is odd, thenans := maximum of ans, (left_1 + right_0 + 1) and (left_0 + right_1 ... Read More

Find Length of Longest Common Subsequence of Three Strings in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:36:12

692 Views

Suppose we have three strings s1, s2, and s3, we have to find the length of their longest common subsequence.So, if the input is like s1 = "ababchemxde" s2 = "pyakcimde" s3 = "oauctime", then the output will be 4, as the longest common subsequence is "acme".To solve this, we will follow these steps −m := size of s1, n := size of s2, o := size of s3dp := a 3D matrix of size (o + 1) x (n + 1) x (m + 1)for i in range 1 to m, dofor j in range 1 to n, dofor ... Read More

Find Length of Longest Arithmetic Subsequence in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:34:52

375 Views

Suppose we have a list of numbers called nums, we have to find the length of the longest arithmetic subsequence. As we know a sequence S[i] is an arithmetic sequence when S[i+1] - S[i] have the same value for every i in range (0 ≤ i < Size of S - 1).So, if the input is like nums = [1, 4, 7, 10, 13, 20, 16], then the output will be 6, the subsequence [1, 4, 7, 10, 13, 16] is an arithmetic because the difference between each consecutive element is 3.To solve this, we will follow these steps −n := size of arrif n

Find Length of Longest Alternating Subsequence in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:32:00

498 Views

Suppose we have a list of numbers called nums, we have to find the size of the longest subsequence where the difference between two consecutive numbers alternate between positive and negative. And the first difference may be either positive or negative.So, if the input is like nums = [6, 10, 4, 2, 3, 9, 4, 7], then the output will be 6, as on possible required subsequence is [6, 10, 2, 9, 4, 7] and the differences are [4, -8, 7, -5, 3].To solve this, we will follow these steps &minuS;n := size of numsdp := a list of size ... Read More

Check If We Can Split List into Consecutive Increasing Sublists in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:30:32

400 Views

Suppose we have a list of numbers called nums and that is sorted in non-decreasing order, we have to check whether it can be split into any number of subsequences such that each subsequence has at minimum length of 3 and that is consecutively increasing.So, if the input is like nums = [2, 3, 4, 4, 5, 6, 7], then the output will be True, as we can split the list to [2, 3, 4] and [4, 5, 6, 7].To solve this, we will follow these steps −counts := A map that contains elements of nums and its countsstarts := ... Read More

Split Lists into Strictly Increasing Sublists in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:27:46

200 Views

Suppose we have a list of numbers called nums, and another value k, we have to check whether it is possible to split the list into sublists lists such that each sublist has length ≥ k and that is strictly increasing. The list does not need to be split contiguously.So, if the input is like nums = [6, 7, 5, 10, 13] k = 2, then the output will be True, as the split is [5, 6] and [7, 10, 13].To solve this, we will follow these steps −c := A map that contains elements of nums and its countsmax_count := maximum of all frequencies of creturn True when max_count * k

Find Largest Sum of 3 Non-Overlapping Sublists of Same Sum in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:26:38

161 Views

Suppose we have a list of numbers called nums and another value k, we have to find the largest sum of three non-overlapping sublists of the given list of size k.So, if the input is like nums = [2, 2, 2, -6, 4, 4, 4, -8, 3, 3, 3] k = 3, then the output will be 27, as we can select the sublists [2, 2, 2], [4, 4, 4], and [3, 3, 3], total sum is 27.To solve this, we will follow these steps −P := [0]for each x in A, doinsert P[-1] + x at the end of ... Read More

Find Maximum Sum of Two Equal Sets in C++

Arnab Chakraborty
Updated on 12-Dec-2020 10:23:56

172 Views

Suppose we have a list of numbers called nums, now find two sets as their sums are same and maximum, then find sum value.So, if the input is like nums = [2, 5, 4, 6], then the output will be 6, as the sets are [2, 4] and [6].To solve this, we will follow these steps −sum := 0for each number i in nums, dosum := sum + in := size of numsDefine one 2D array dp of size (n + 1) x (2 * sum + 5) and fill with -1dp[0, sum] := 0for initialize i := 1, when ... Read More

Find Largest Binary Search Subtree from a Given Tree in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:19:16

171 Views

Suppose we have a binary tree, we have to find the largest subtree (with maximum number of nodes) as binary search tree.So, if the input is likethen the output will beTo solve this, we will follow these steps −max_size := [0]max_node := [null]Define a function traverse() . This will take nodeif node is null, thenreturn nullleft := traverse(left of node)right := traverse(right of node)lst := left + [value of node] + rightif lst is sorted, thenif max_size[0] < size of lst, thenmax_size[0] := size of lstmax_node[0] := nodereturn lsttraverse(root)From the main method return max_node[0]Example (Python)Let us see the following implementation ... Read More

Number of Ways to Reach Bottom Right Point in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:16:40

401 Views

Suppose we have one N x M binary matrix. Where 0 means empty cell and 1 means blocked cell. Now starting from the top left corner, we have to find the number of ways to reach the bottom right corner. If the answer is very large, mod it by 10^9 + 7.So, if the input is like001000110then the output will be 2, as There are two ways to get to the bottom right: [Right, Down, Right, Down] and [Down, Right, Right, Down].To solve this, we will follow these steps −dp := a matrix of same size of given matrix and ... Read More

Advertisements