Server Side Programming Articles - Page 1869 of 2650

Maximum Sum Circular Subarray in C++

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

290 Views

Suppose we have a circular array C of integers represented by A, we have to find the maximum possible sum of a non-empty subarray of C. Also, a subarray may only include each element of the fixed buffer A at most once. If the array is like [1, -2, 3, -2], then the output will be 3. This is because subarray[3] has maximum sum 3.To solve this, we will follow these steps −n := size of vcreate arrays leftSum, leftSumMax, rightSum, rightSumMax all of size nleftSum[0] := v[0], leftSumMax[0] := maximum of 0 and v[0]for i in range 1 to ... Read More

Binary Tree Pruning in C++

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

689 Views

Suppose we have the head node root of a binary tree, where additionally every node's value is either a 0 or a 1. We have to find the same tree where every subtree not containing a 1 has been deleted. So if the tree is like −To solve this, we will follow these steps −Define a recursive method solve(), this will take the node. the method will be like −If node is null, then return nullleft of node := solve(left of node)right of node := solve(right of node)if left of node is null and right of node is also null ... Read More

Maximum Width of Binary Tree in C++

Arnab Chakraborty
Updated on 02-May-2020 09:59:28

216 Views

Suppose we have a binary tree, we have to define a function to get the maximum width of the given tree. Here the width of a tree is the maximum width among all levels. We will consider the binary tree has the same structure as a full binary tree, but some nodes are null. The width of one level is actually the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted for the length calculation). So if the tree is like −Then the maximum width ... Read More

Maximum Binary Tree in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:31:49

650 Views

Suppose we have an integer array. All elements in that array is unique. A maximum tree building on this array is defined as follow −The root will hold the maximum number in the array.The left subtree is the maximum tree constructed from left side of the subarray divided by the maximum number.The right subtree is the maximum tree constructed from right side of subarray divided by the maximum number.We have to construct the maximum binary tree. So if the input is like: [3, 2, 1, 6, 0, 5], then the output will be −To solve this, we will follow these ... Read More

Find Largest Value in Each Tree Row in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:27:57

202 Views

Suppose we have a binary tree, we have to find the largest elements of each level of that tree. So if the tree is like −Then the output will be [3, 5, 8]To solve this, we will follow these steps −Define an array called ansdefine a recursive function solve(), this will take tree node, and level, the level is initially 0. this method will act like −if node is null, then returnif level = size of ans, then insert node value into ans, otherwise ans[level] := max of ans[level] and node valuecall solve(left subtree of node, level + 1)call solve(right ... Read More

Find Bottom Left Tree Value in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:25:32

245 Views

Suppose we have a binary tree. We have to find the left most value of the last row of that tree. So if the tree is like −Then the output will be 7, as the last row is [7, 4], and left most element is 7.To solve this, we will follow these steps −initially define ans and lvl variable as 0define one method called solve(), this will take the tree node, and level, the level is initially 0. This will act as follows −if node is null, then returnif level > lvl, then ans := value of node and lvl ... Read More

Most Frequent Subtree Sum in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:24:15

333 Views

Suppose we have the root of a tree, we have to find the most frequent subtree sum. The subtree sum of a node is actually the sum of all the node values formed by the subtree rooted at that node (including the node itself). The most frequent subtree sum is actually If there is a tie, return all the values with the highest frequency in any order. So if the tree is like [5, 2, -5], then it will return [2]. This is because 2 happens twice, however -5 only occurs once.To solve this, we will follow these steps −Define ... Read More

Circular Array Loop in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:16:09

3K+ Views

Suppose we have a circular array nums of positive and negative integer values. If a number k at an index is a positive number, then move forward k steps. Otherwise, if it's negative (-k), move backward k steps. Since the array is circular, we can assume that the next element of the last element is the first element, and the previous element of the first element is the last element. We have to check whether if there is a loop (or a cycle) in nums. A cycle must start and end at the same index and the cycle's length > ... Read More

Minimum Number of Arrows to Burst Balloons in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:15:00

315 Views

Suppose there are few spherical balloons spread in two-dimensional space. For each balloon, there are the start and end coordinates of the horizontal diameter. Start is always smaller than end. There will be at most 104 balloons. One arrow can be shot up exactly vertically from different points along the x-axis. A balloon whose position is xstart to xend bursts by an arrow shot at x if xstart = x = xend. There is no limit to the number of arrows that can be shot. Assume that an arrow once shot keeps travelling up infinitely. We have to find the ... Read More

Delete Node in a BST in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:05:03

4K+ Views

Suppose we have a binary search tree. We will take one key k, and we have to delete the given key k from the BST, and return the updated BST. So if the tree is like −And the key k = 3, then the output tree will be −To solve this, we will follow these steps −Define a method called deleteRoot() to delete the root node, this will work as followsif root is null, then return nullif root has no right subtree, then return left of rootx := inorder successor of rootset left of x as left := left of ... Read More

Advertisements