Found 26504 Articles for Server Side Programming

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

Capacity To Ship Packages Within D Days in Python

Arnab Chakraborty
Updated on 02-May-2020 10:46:47

337 Views

Suppose there is a conveyor belt has packages that will be shipped from one port to another within D days. Here the i-th package on the conveyor belt has a weight of weights[i]. Each day, we will load the ship with packages on the belt. We will not load more weight than the maximum weight capacity of the ship. We have to find the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within D days. So if the input is like [3, 2, 2, 4, 1, 4] and D ... Read More

Construct Binary Search Tree from Preorder Traversal in Python

Arnab Chakraborty
Updated on 02-May-2020 10:45:57

805 Views

Suppose we have to create a binary search tree that matches the given preorder traversal. So if the pre-order traversal is like [8, 5, 1, 7, 10, 12], then the output will be [8, 5, 10, 1, 7, null, 12], so the tree will be −To solve this, we will follow these steps −root := 0th node of the preorder traversal liststack := a stack, and push root into the stackfor each element i from the second element of the preorder listi := a node with value iif value of i < top of the stack top element, thenleft of ... Read More

Check If Word Is Valid After Substitutions in C++

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

342 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

Smallest String Starting From Leaf in Python

Arnab Chakraborty
Updated on 02-May-2020 10:35:23

276 Views

Suppose we have the root of a binary tree, each node is containing a value from 0 to 25 , which are representing the letters 'a' to 'z': a value of 0 represents 'a', a value of 1 represents 'b', and so on. We have to search the lexicographically smallest string that starts at a leaf of this tree and finishes at the root. So if the tree is like −Then the output will be “adz” as the sequence is [0, 3, 25]To solve this, we will follow these steps −Define a dfs traversal method as followsif node is not ... 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

490 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

228 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

645 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

Advertisements