Found 10784 Articles for Python

Program to check whether each node value except leaves is sum of its children value or not in Python

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

241 Views

Suppose we have a binary tree, we have to check whether for every node in the tree except leaves, its value is same as the sum of its left child's value and its right child's value or not.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps −Define a function dfs() . This will take rootif root is null, thenreturn Trueif left of root is null and right of root is null, thenreturn Trueleft := 0if left of root is not null, thenleft := value of left of rootright := 0if right ... Read More

Program to find three unique elements from list whose sum is closest to k Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:43:13

178 Views

Suppose we have a list of numbers called nums and another value k, we have to find three unique entries in nums (a, b, c) such that |a + b + c − k| is minimized and return the absolute difference.So, if the input is like nums = [2, 5, 25, 6] k = 14, then the output will be 1 as if we take [2, 5, 6] will get us closest to 14 and the absolute difference is |13 − 14| = 1.To solve this, we will follow these steps −sort the list numsans := 1^9for i in range ... Read More

Program to check number of triplets from an array whose sum is less than target or not Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:40:48

145 Views

Suppose we have a list of numbers called nums and another value target, we have to find the number of triples (i < j < k) that exist such that nums[i] + nums[j] + nums[k] < target.So, if the input is like nums = [−2, 6, 4, 3, 8], target = 12, then the output will be 5, as the triplets are: [−2, 6, 4], [−2, 6, 3], [−2, 4, 3], [−2, 4, 8], [−2, 3, 8]To solve this, we will follow these steps −sort the list numsans := 0n := size of numsfor i in range 0 to n−1, ... Read More

Program to check we can find three unique elements ose sum is same as k or not Python

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

59 Views

Suppose we have a list of numbers called nums and another value k, we have to check whether we can find three unique elements in the list whose sum is k.So, if the input is like nums = [11, 4, 6, 10, 5, 1] k = 20, then the output will be True, as we have numbers [4, 6, 10] whose sum is 20.To solve this, we will follow these steps −sort the list numsl := 0, r := size of nums − 1while l < r − 1, dot := k − nums[l] − nums[r]if nums[r − 1] < ... Read More

Program to check we can find four elements whose sum is same as k or not in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:31:22

70 Views

Suppose we have a list of numbers called nums and a value k, we have to check whether there are four unique elements in the list that add up to k.So, if the input is like nums = [11, 4, 6, 10, 5, 1] k = 25, then the output will be True, as we have [4, 6, 10, 5] whose sum is 25.To solve this, we will follow these steps −sort the list numsn := size of numsfor i in range 0 to n − 4, dofor j in range i + 1 to n − 3, dol := ... Read More

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

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

293 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

113 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

221 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

216 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

170 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

Advertisements