Found 10476 Articles for Python

Program to check whether one tree is subtree of other or not in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:21:36

374 Views

Suppose we have two binary trees. We have to check whether second tree is a subtree of first one or not.So, if the input is likethen the output will be True.To solve this, we will follow these steps −Define a function solve() . This will take root, targetif root is null and target is also null, thenreturn Trueif root is null or target is null, thenreturn Falseif value of root is same as value of target, thenreturn solve(left of root, left of target) and solve(right of root, right of target)otherwise, return solve(left of root, target) or solve(right of root, target)Let ... Read More

Program to find maximum sum of the subsequence, where difference of two values is same as their position difference in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:19:19

190 Views

Suppose we have a list of numbers called nums, we select a subsequence of strictly increasing values, where the differences of each two numbers is the same as the differences of their two indices. So we have to find the maximum sum of such a subsequence.So, if the input is like nums = [6, 7, 9, 9, 8, 5], then the output will be 22, as we select the subsequence [6, 7, 9] whose indices are [0, 1, 3]. The differences between each consecutive numbers is [1, 2] which is same as the differences of their indices.To solve this, we ... Read More

Program to check sublist sum is strictly greater than the total sum of given list Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:15:54

288 Views

Suppose we have a list of numbers called nums, we have to check whether there is a sublist such that its sum is strictly greater than the total sum of the list.So, if the input is like nums = [1, −2, 3, 4], then the output will be True, as the sum of the list is 6 and the sum of the sublist [3, 5] is 8 which is strictly larger.To solve this, we will follow these steps −total := sum of elements numss := 0for each i in nums, dos := s + iif s < 0, thenreturn Trues ... Read More

Program to check maximum sum of all stacks after popping some elements from them in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:11:52

297 Views

Suppose we have a list of stacks, we can take any stack or stacks and pop any number of elements from it. We have to find the maximum sum that can be achieved such that all stacks have the same sum value.So, if the input is like stacks = [[3, 4, 5, 6], [5, 6, 1, 4, 4], [10, 2, 2, 2] ], then the output will be 12, as we can take operations like −Pop [6] from the first stack we get [3, 4, 5] the sum is 12.Pop [4, 4] from the second stack we get [5, 6, ... Read More

Program to find product of few numbers whose sum is given in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:09:45

224 Views

Suppose we have a number n, we have to find two or more numbers such that their sum is equal to n, and the product of these numbers is maximized, we have to find the product.So, if the input is like n = 12, then the output will be 81, as 3 + 3 + 3 + 3 = 12 and 3 * 3 * 3 * 3 = 81.To solve this, we will follow these steps −Define a function dp() . This will take nif n is same as 0, thenreturn 1ans := 0for i in range 1 to ... Read More

Program to sort an array based on the parity values in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:07:37

370 Views

Suppose, we have an array A with few integers. We have to sort the numbers as even then odd. So put the even numbers at first, then the odd numbers. So if the array is like A = [1, 5, 6, 8, 7, 2, 3], then the result will be like [6, 8, 2, 1, 5, 7, 3]To solve this, we will follow these steps −set i := 0 and j := 0while j < size of arrif arr[j] is even, thenswap arr[i] and arr[j], increase i by 1increase j by 1return arrLet us see the following implementation to get ... Read More

Program to find smallest intersecting element of each row in a matrix in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:06:05

175 Views

Suppose we have a 2D matrix where each row is sorted in ascending order. We have to find the smallest number that exists in every row. If there's no such result, then return −1.So, if the input is like23551010135then the output will be 5To solve this, we will follow these steps −if matrix is empty, thenreturn −1first := a new set from first row of matrixfor each row in matrix, dofirst := Intersect first a set of elements of rowif first is empty, thenreturn −1return minimum of firstLet us see the following implementation to get better understanding −Example Live Democlass Solution: ... Read More

Program to find sibling value of a binary tree node in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:03:46

665 Views

Suppose we have a value k and a binary search tree, here each node is either a leaf or contains 2 children. We have to find the node containing the value k, and return its sibling's value.So, if the input is likek = 4., then the output will be 10.To solve this, we will follow these steps −Define a function util() . This will take root, k, ansif left of root is not null and right of root is not null, thenreturnif k > value of root, thenif value of right of root is same as k, theninsert value of ... Read More

Program to split a set into equal sum sets where elements in first set are smaller than second in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:00:47

854 Views

Suppose we have a list of numbers called nums, we have to check whether we can divide the list into two groups A and B such that: The sum of A and the sum of B are same. Here every number in A is strictly smaller than every number in B.So, if the input is like nums = [3, 4, 5, 12], then the output will be True, as we can have A = [3, 4, 5] and B = [12] and both have sum 12.To solve this, we will follow these steps −sort the list numstotal := sum of ... Read More

Program to count total number of set bits of all numbers in range 0 to n in Python

Arnab Chakraborty
Updated on 21-Oct-2020 10:58:45

219 Views

Suppose we have a number num. For each numbers i in the range 0 ≤ i ≤ num we have to calculate the number of 1's in their binary counterpart and return them as a list. So if the number is 5, then the numbers are [0, 1, 2, 3, 4, 5], and number of 1s in these numbers are [0, 1, 1, 2, 1, 2], so it will return 7.To solve this, we will follow these steps −res := an array which holds num + 1 number of 0soffset := 0for i in range 1 to num + 1if ... Read More

Advertisements