Find Sum of Right Leaves of a Binary Tree in C++

Arnab Chakraborty
Updated on 21-Oct-2020 11:33:29

194 Views

Suppose we have a binary tree we have to find the sum of all right leaves in a given binary tree.So, if the input is likethen the output will be 17, as there are two right leaves in the binary tree, with values 7 and 10 respectively.To solve this, we will follow these steps −Define a function dfs(), this will take node, add, if node is null, then −returnif left of node is null and right of node is null and add is non-zero, then −ret := ret + val of nodedfs(left of node, false)dfs(right of node, true)From the main ... Read More

Check if Four Elements Sum to K in Python

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

126 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

Solve Partially Filled Sudoku Grid in C++

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

323 Views

Suppose we have a partially filled Sudoku grid and we have to solve this. We know that Sudoku is a 9 × 9 number grid, and the whole grid are also divided into 3 × 3 boxes There are some rules to solve the Sudoku.We have to use digits 1 to 9 for solving this problem.One digit cannot be repeated in one row, one column or in one 3 × 3 box.Using backtracking algorithm, we will try to solve Sudoku problem. When some cell is filled with a digit, it checks whether it is valid or not. When it is ... Read More

Check if One Tree is Subtree of Another in Python

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

375 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

Find Maximum Sum of Subsequence in Python

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

193 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

Check If a String is Subsequence of Another in C++

Arnab Chakraborty
Updated on 21-Oct-2020 11:17:35

2K+ Views

Suppose we have two strings S and T. We have to check whether S is subsequence of T or not.So, if the input is like S = "abc", T = "adbrcyxd", then the output will be TrueTo solve this, we will follow these steps −if s is same as t, then −return truen := size of s, m := size of tj := 0for initialize i := 0, when i < n, update (increase i by 1), do −if t[j] is same as s[i], then −(increase j by 1)if j is same as size of t, then −return truereturn falseLet ... Read More

Check Sublist Sum is Greater than Total Sum in Python

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

291 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

Multiply Two Strings and Return Result as String in C++

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

1K+ Views

Suppose we have two numbers as string. We have to multiply them and return the result also in string. So if the numbers are “28” and “25”, then the result will be “700”To solve this, we will follow these steps −Taking two arguments x and y it indicates x divides yif x < −Infinity and y = 1, then return infinitya := |x|, b := |y| and ans := 0while a − b >= 0p := 0while a − (left shifted b (left shifted 1 p times)) >= 0p := p + 1a := a − (left shift b, p ... Read More

Check Maximum Sum of All Stacks After Popping Elements in Python

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

299 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

Find Product of Numbers Whose Sum is Given in Python

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

225 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