Found 7197 Articles for C++

Find Elements in a Contaminated Binary Tree in C++

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

205 Views

Suppose we have a binary tree. The rules of that tree is as follows −root.val == 0If treeNode.val is x and treeNode.left is not null, then treeNode.left.val = 2 * x + 1If treeNode.val is x and treeNode.right is not null, then treeNode.right.val = 2 * x + 2Now as the binary tree is contaminated. This indicates all val of the tree node have been changed to -1. We have to first recover the binary tree and then implement the FindElements class as follows −FindElements(TreeNode* root) Initializes the object with a contamined binary tree, we have to recover it first.bool ... Read More

Number of Closed Islands in C++

Arnab Chakraborty
Updated on 02-May-2020 11:05:44

292 Views

Suppose we have a 2D grid consists of 0s (as land) and 1s (as water). An island is a maximal 4- directionally connected group of 0s. A closed island is an island totally surrounded by 1s. We have to find the number of closed islands. So if the grid is like1111111010000110101011101000010111111110So the output will be 2. There are two islands, that are completely surrounded by water.To solve this, we will follow these steps −Define a variable flagDefine a method called dfs, this will take the grid, i, j, n and mif i and j are not inside the range of ... Read More

Minimum Remove to Make Valid Parentheses in C++

Arnab Chakraborty
Updated on 02-May-2020 11:07:25

445 Views

Suppose we have a string s of '(' , ')' and lowercase English characters. We have to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parenthese string is valid and return any valid string. A parentheses string is valid when all of these criteria are fulfilled −It is the empty string, contains lowercase characters only, orIt can be written as the form of AB (A concatenated with B), where A and B are valid strings, orIt can be written as the form of (A), where A is a valid string.So ... Read More

Shortest Path in Binary Matrix in C++

Arnab Chakraborty
Updated on 02-May-2020 11:01:56

833 Views

Suppose we have an N by N square grid, there each cell is either empty (0) or blocked (1). A clear path from top-left to bottom-right has length k if and only if it is composed of cells C_1, C_2, ..., C_k such that −Adjacent cells C_i and C_{i+1} are connected 8-directionally (So they are different and share an edge or corner)C_1 is at location (0, 0)C_k is at location (N-1, N-1)If C_i is located at (r, c), then grid[r, c] is empty or contains 0We have to find the length of the shortest such clear path from top-left to ... Read More

Binary Search Tree to Greater Sum Tree in C++

Ravi Ranjan
Updated on 04-Sep-2025 15:02:56

438 Views

In this article, we are given a binary search tree. Our task is to transform the given BST to a Greater Sum tree. A Greater Sum Tree with respect to the given BST is a tree where each node's value is replaced by the sum of all values greater than that node's value in the original BST. Below is an example scenarios to convert the given BST to a Greater Sum Tree: Example Scenario Input: BST inorder traversal= {0, 1, 2, 3, 4, 5, 6, 7, 8} Output: Greater Sum Tree = {36, 35, 33, 30, 26, 21, 15, ... Read More

Maximum Difference Between Node and Ancestor in C++

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

193 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

341 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

194 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

295 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

489 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

Advertisements