Split Array with Equal Sum in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:05:16

864 Views

Suppose we have an array with n integers, we have to find if there are triplets (i, j, k) which follows these conditions −0 < i, i + 1 < j, j + 1 < k < n - 1Sum of subarrays (0, i - 1), (i + 1, j - 1), (j + 1, k - 1) and (k + 1, n - 1) will be same.The subarray (L, R) is a slice of the original array starting from the element indexed L to the element indexed R.So, if the input is like [1, 2, 1, 2, 1, 2, ... Read More

Boundary of Binary Tree in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:02:36

224 Views

Suppose we have a binary tree, we have to find the values of its boundary in anti-clockwise direction starting from root. Here boundary includes left boundary, leaves, and the right boundary in order without duplicate nodes.The left boundary is the path from root to the left-most node.The Right boundary is the path from root to the right-most node.When the root doesn't have left subtree or right subtree, then the root itself is left boundary or right boundary.So, if the input is likethen the output will be [1, 2, 4, 7, 8, 9, 10, 6, 3]To solve this, we will follow ... Read More

Output Contest Matches in C++

Arnab Chakraborty
Updated on 16-Nov-2020 13:58:18

207 Views

Suppose we have n teams and we always arrange the rather strong team to play with the rather weak team, like make the rank 1 team play with the team with rank n, this strategy is to make the contest more interesting. Now we have to find their final contest matches in the form of a string.These teams are given in the form of positive integers from 1 to n, which represents their initial rank. So, rank 1 is the strongest team, and Rank n is the weakest team. We will use parentheses and commas to represent the contest team ... Read More

Construct Binary Tree from String in C++

Arnab Chakraborty
Updated on 16-Nov-2020 13:56:15

2K+ Views

Suppose we have a string consisting of parenthesis and integers. We have to construct a binary tree from that string. The whole input represents a binary tree. It holds an integer that followed by zero, one or two pairs of parentheses. The integer represents the root's value and a pair of parenthesis contains a child binary tree with the same structure.So, if the input is like "4(2(3)(1))(6(5))", then the output will be [3, 2, 1, 4, 5, 6] (inorder traversal)To solve this, we will follow these steps −Define a function solve(), this will take s, idx, if idx >= size ... Read More

Lonely Pixel II in C++

Arnab Chakraborty
Updated on 16-Nov-2020 13:52:40

170 Views

Suppose we have a picture consisting of black and white pixels, we have to find the number of black pixels, which are present at row R and column C. That is aligned with all the following rules −R and C will contain exactly N black pixelsFor all rows, that have a black pixel at column C, they should be exactly the same as row R.Here the picture is represented by a 2D char array consisting of 'B' and 'W', for the black and white pixels respectively.If the input is like −WBWBBWWBWBBWWBWBBWWWBWBWAnd N = 3, then the output will be 6. ... Read More

Rotting Oranges in C++

Arnab Chakraborty
Updated on 16-Nov-2020 13:48:11

239 Views

Suppose we have a grid, here in each cell can have one of three values −value 0 for an empty cell;value 1 for a fresh orange;value 2 for a rotten orange.In every minute, any fresh orange that is adjacent to a rotten orange becomes rotten.We have to find the minimum number of times that must elapse until no cell has a fresh orange. If this is not possible, then return -1.So, if the input is like [[2, 1, 1], [1, 1, 0], [0, 1, 1]], then the output will be 4To solve this, we will follow these steps −minutes := ... Read More

Cousins in Binary Tree in C++

Arnab Chakraborty
Updated on 16-Nov-2020 13:32:53

539 Views

Suppose we have a binary tree, the root node is present at depth 0, and children of each depth k node are at depth k+1.Here two nodes of a binary tree are called cousins if they have the same depth, but have different parents.All values of the tree will be unique, and the values x and y of two different nodes in the tree. We have to check whether the nodes corresponding to the values x and y are cousins or not.So, if the input is likex = 5, y = 4, then the output will be trueTo solve this, ... Read More

Insert Item in Sorted List While Maintaining Order in Python

Hafeezul Kareem
Updated on 13-Nov-2020 19:17:11

8K+ Views

In this article, we are going to learn how to insert an item in a sorted list maintaining the order. Python has a built-in module called bisect that helps us to insert any element in an appropriate position in the list.Follow the below steps to write the code.Import the module bisect.Initialize list and element that need to insertThe module bisect has a method called insort that inserts an element into a list in an appropriate position. Use the method and insert the element.Print the list.Example Live Demo# importing the module import bisect # initializing the list, element numbers = [10, ... Read More

Intersect Two Dictionaries Through Keys in Python

Hafeezul Kareem
Updated on 13-Nov-2020 19:13:25

2K+ Views

In this article, we are going to learn how to intersect two dictionaries using keys. We have to create a new dictionary with common keys. Let's see an example.Input: dict_1 = {'A': 1, 'B': 2, 'C': 3} dict_2 = {'A': 1, 'C': 4, 'D': 5} Output: {'A': 1, 'C': 3}We are going to use the dictionary comprehension to solve the problem. Follow the below steps to write the code.Initialize dictionaries.Iterate over the dictionary one and add elements that are not in dictionary two.Print the result.Example Live Demo# initializing the dictionaries dict_1 = {'A': 1, 'B': 2, 'C': 3} dict_2 = ... Read More

Intersection of Multiple Lists in Python

Hafeezul Kareem
Updated on 13-Nov-2020 19:08:20

1K+ Views

In this article, we are going to see how to intersect two lists that contain multiple lists in different ways. Let's start in the traditional way.Follow the below the steps to solve the problemInitialize two lists with multiple listsIterate over the first list and add the current item in the new list if it presents in the second list as well.Print the result.Example Live Demo# initializing the lists list_1 = [[1, 2], [3, 4], [5, 6]] list_2 = [[3, 4]] # finding the common items from both lists result = [sub_list for sub_list in list_1 if sub_list in list_2] ... Read More

Advertisements