Found 7347 Articles for C++

Binary Search Tree to Greater Sum Tree in C++

Arnab Chakraborty
Updated on 02-May-2020 10:59:41

190 Views

Suppose we have the root of a binary search tree with distinct values, we have to modify it so that every node has a new value equal to the sum of the values of the original tree that are greater than or equal to the value of node. We have to keep in mind that we are dealing with binary search tree, and this should maintain the properties of the BST. So if the input tree is like −Then the output tree will be −To solve this, we will follow these steps −set global := 0Define a recursive function solve(), ... Read More

Maximum Difference Between Node and Ancestor in C++

Arnab Chakraborty
Updated on 02-May-2020 10:51:08

148 Views

Suppose we have the root of a binary tree, we have to find the maximum value V for which there exists different nodes A and B where V = |value of A – value of B| and A is an ancestor of B. So if the tree is like −Then the output will be 7. The ancestor node differences are like [(8 - 3), (7 - 3), (8 - 1), (10-13)], among them the (8 - 1) = 7 is the maximum.To solve this, we will follow these steps −initially define ans 0define one method called solve(), this will take ... Read More

Check If Word Is Valid After Substitutions in C++

Arnab Chakraborty
Updated on 02-May-2020 10:44:26

251 Views

Suppose we are given that the string "abc" is valid. So from any valid string V, we can divide V into two pieces X and Y such that X + Y is same as V. (X or Y may be empty.). Then, X + "abc" + Y is also valid. So for example S = "abc", then examples of valid strings are: "abc", "aabcbc", "abcabc", "abcabcababcc". And some examples of invalid strings are: "abccba", "ab", "cababc", "bac". We have to check true if and only if the given string S is valid.So if the input is like “abcabcababcc”, then that ... Read More

Maximum Binary Tree II in C++

Arnab Chakraborty
Updated on 02-May-2020 10:43:36

118 Views

Suppose we have a root node of a maximum tree: The maximum tree is a tree where every node has a value greater than any other value in its subtree. Suppose we have a method called construct(). This can construct a root from a list A. The construct() method is like −If list A is empty, return null.Otherwise, let A[i] be the largest element of the list A. Then create a root node with value A[i].The left child of root will be construct([A[0], A[1], ..., A[i-1]])The right child of root will be construct([A[i+1], A[i+2], ..., A[n - 1]]) [n is ... Read More

Interval List Intersections in C++

Arnab Chakraborty
Updated on 02-May-2020 10:36:38

206 Views

Suppose we have two lists of closed intervals, here each list of intervals is pairwise disjoint and in sorted order. We have ti find the intersection of these two interval lists.We know that the closed interval [a, b] is denoted as a A[i][1] or A[i][1] B[j][1] or B[j][1] B[j][1]:          j+=1       else:          i+=1    return result    def intersect(self,a,b):       if a[0]=b[0]:          return True       if b[0]=a[0]:          return True       return False ob = Solution() print(ob.intervalIntersection([[0,2],[5,10],[13,23],[24,25]],[[1,5],[8,12],[15,24],[25,27]]))Input[[0,2],[5,10],[13,23],[24,25]] [[1,5],[8,12],[15,24],[25,27]]Output[[1, 2], [5, 5], [8, 10], [15, 23], [24, 24], [25, 25]]

Minimum Cost For Tickets in C++

Arnab Chakraborty
Updated on 02-May-2020 10:27:40

363 Views

Suppose there is a country, that is popular for train travel, we have planned some train travelling one year in advance. We have an array, that is holding the days of the year that we will travel. Each day is an integer from 1 to 365. Train tickets are sold in three different ways −A 1-day pass is sold for costs[0] dollars;A 1-day pass is sold for costs[0] dollars;A 30-day pass is sold for costs[2] dollars.Here the passes allow that many days of consecutive travel. For example, if we get one 7-day pass on day 2, then we can travel ... Read More

Distribute Coins in Binary Tree in C++

Arnab Chakraborty
Updated on 02-May-2020 10:22:04

168 Views

Suppose we have the root of a binary tree with N nodes, here each node in the tree has node.val number of coins, and there are N coins in total. In one move, we can choose two adjacent nodes and move only one coin from one node to the another node. (The move may be from parent to child node, or from child to parent node.). We have to find the number of moves required to make every node have exactly one coin.So if the tree is like −Then the output will be 3. From the left child, send 2 ... Read More

Pancake Sorting in C++

Arnab Chakraborty
Updated on 02-May-2020 10:18:14

452 Views

Suppose we have an array A, we will perform the Pancake sort technique on A. Here the main constraint is that we can use only one operation called rev(arr, i). This will reverse the elements of arr from 0 to ith position. This idea is like the selection sort. We repeatedly place the max element at end reduce the size of the array. So if the input is like [54, 85, 52, 25, 98, 75, 25, 11, 68], then the result will be [11, 25, 25, 52, 54, 68, 75, 85, 98]To solve this, we will follow these steps −size ... Read More

Check Completeness of a Binary Tree in C++

Arnab Chakraborty
Updated on 02-May-2020 10:17:15

153 Views

Suppose we have a binary tree. We have to check whether the tree is a complete binary tree or not. A complete binary tree of level n, has n-1 complete levels, and all nodes at level n, are filled from the left. So if the input tree is like −Then the output will be true, As this is complete binary tree.To solve this, we will follow these steps −If tree is empty, then return nullmake a queue q and insert root into itset flag := truewhile q has some elementssz := size of the queuewhile sz is not 0node := ... Read More

Flip Equivalent Binary Trees in C++

Arnab Chakraborty
Updated on 02-May-2020 10:11:36

232 Views

Suppose we have a binary tree. We have to flip the binary tree. The flip indicates: choose any node, and swap the left and right child subtrees. Now a binary tree X is flip equivalent to a binary tree Y if and only if we can make Y from X, after some number of flip operations. We have to write a method that determines whether two binary trees are flip equivalent. The trees are given by root nodes root1 and root2. So if the trees are −Then the output will be true, if we flip nodes with values 1, 3 ... Read More

Advertisements