Found 7347 Articles for C++

Program to count number of configurations are there to fill area with dominos and trominos in C++

Arnab Chakraborty
Updated on 25-Dec-2020 05:58:00

68 Views

Suppose we have two shapes, Domino and Tromino. Dominos are 2 x 1 shape and Trominos are ‘L’ like shape. They can be rotated like below −If we have a number n, we have to find number of configurations to fill a 2 x n board with these two types of pieces. As we know in tiling, every square must be covered by a tile.So if the input is 3, then the output will be 5. So the arrangements can be [XYZ XXZ XYY XXY XYY] and [XYZ YYZ XZZ XYY XXY], here different letters are used for different tiles.To ... Read More

Program to find top view of a binary tree in Python

Arnab Chakraborty
Updated on 25-Dec-2020 05:55:52

277 Views

Suppose we have a binary tree, we have to find the top view of the tree, they will be sorted left−to−right.So, if the input is like image, then the output will be [3, 5, 8, 6, 9], as 3 is above 2 and 5 is above 7 so they are not visible.To solve this, we will follow these steps −view := a new empty mapq := a double ended queueinsert pair (root, 0) at the end of qstart := inf, end := −infwhile q is not empty, do(node, coord) := left element of q, then remove left element of qstart ... Read More

Program to find number of minimum steps required to meet all person at any cell in Python

Arnab Chakraborty
Updated on 25-Dec-2020 05:53:06

98 Views

Suppose we have a 2D matrix where these values are present: 0 represents an empty cell. 1 represents a wall. 2 represents a person. Now a person can walk any of the four direction of up, down, left, right otherwise stay in one time unit. We have to find a walkable cell such that it minimizes the time it would take for everyone to meet and return the time. We have to keep in mind that two people can walk through the same empty cell and you can assume there is always some path between any two people.So, if the ... Read More

Program to find minimum number of steps required to catch the opponent in C++

Arnab Chakraborty
Updated on 25-Dec-2020 05:49:05

130 Views

Suppose we have a list of tree edges in the form [u, v], this indicates there is an undirected edge between u and v. And we also have two values x and y. If we are at node x, and our opponent is at node y. In the first round, we move, then in the next round the opponent moves and so on. The opponent can select to not make a move in a round. We have to find the minimum number of rounds it we need to catch the opponent.So, if the input is like edges = [[0, 1], ... Read More

Program to find minimum number of swaps needed to arrange all pair of socks together in C++

Arnab Chakraborty
Updated on 25-Dec-2020 05:46:28

124 Views

Suppose we have a list of numbers called row and this is representing socks lying in a row. They are not sorted, but we want to rearrange them so that each pair of socks are side by side like (0, 1), (2, 3), (4, 5), and so on. We have to find the minimum number of swaps required to rearrange them.So, if the input is like row = [0, 5, 6, 2, 1, 3, 7, 4], then the output will be 2, as the row orders are[0, 5, 6, 2, 1, 3, 7, 4][0, 1, 6, 2, 5, 3, 7, ... Read More

Program to find sum of medians of all odd length sublists in C++

Arnab Chakraborty
Updated on 25-Dec-2020 05:44:53

194 Views

Suppose we have a list of numbers called nums, we have to find the sum of the medians of every odd−length sublist of the given list.So, if the input is like nums = [2, 4, 6, 3], then the output will be 23, as the odd−length sublists are − [2], [4], [6], [3], [2, 4, 6], [4, 6, 3], so the sum of the medians is 2 + 4 + 6 + 3 + 4 + 4 = 23To solve this, we will follow these steps −ret := 0for initialize i := 0, when i < size of nums, update ... Read More

Program to Find the Shortest Distance Between Two Points in C++

Arnab Chakraborty
Updated on 23-Dec-2020 07:04:51

1K+ Views

Suppose we have a list of coordinates where each element is of the form [x, y], representing Euclidean coordinates. We have to find the smallest squared distance (x1 - x2) 2 + (y1 - y2) 2 between any two provided coordinates.So, if the input is like coordinates = {{1, 2}, {1, 4}, {3, 5}}, then the output will be 4.To solve this, we will follow these steps −Define one map ytorightmostxsort the array coordinatesret := infinityfor each p in cordinates −it = return the value where (p[1] - sqrt(ret)) is in ytorightmostx or the smallest value greater than it from ... Read More

Program to Find Out the Cost after Finding k Unique Subsequences From a Given String in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:51:32

121 Views

Suppose, we have a string s and another value k. We have to select some subsequences of s, so that we can get k unique subsequences. Here, the cost of selecting a subsequence equals the length of (s) - length of (subsequence). So, we have to find the lowest total cost possible after selecting k unique subsequences. If we are unable to find out this set, we will return -1. We will consider the empty string as a valid subsequence.So, if the input is like s = "pqrs", k = 4, then the output will be 3.To solve this, we ... Read More

Program to Find Out the Amount of Rain to be Caught between the Valleys in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:41:37

54 Views

Suppose we have a 2D matrix, where the elements represent the height of a terrain. Let's imagine a situation where it will rain and all the spaces in the valleys get filled up.We have to find out the amount of rain that will be caught between the valleys.So, if the input is like666864586666then the output will be 3 as we can hold 3 units of water in between 4 and 5 squares.To solve this, we will follow these steps −Define a structure Data, that contains x and y coordinate and height hDefine a priority queue pq, it stores data items ... Read More

Program to Find Out the Minimum Parsing Tree in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:35:50

312 Views

Suppose we have a list of unique and sorted numbers that represent breakpoints in a string. We want to create a tree out of these rules −There are nodes that have a value (a, b) where a and b are breakpoints. This means the node spans from indices [a, b] in the string.The root node spans over every breakpoint. (the whole string).The spans of a node's left and right child are ordered, contiguous, and contains the parent node's span.Leaf nodes' index of 'a' in breakpoints is 1 before the index of 'b' in breakpoints.The cost of a tree is determined ... Read More

Advertisements