Found 7197 Articles for C++

Program to construct Maximum Stack with given operations in C++

Arnab Chakraborty
Updated on 26-Dec-2020 11:11:10

409 Views

Suppose we want to make a maximum stack, which supports following operations −MaxStk() this will construct a new instance of a maximum stackpush(val) inserts val to the stacktop() get the top most element from stackmax() get the maximum element from the stackpop() removes and returns the top most element from the stackpopmax() removes and returns the maximum element from the stackNow construct the maximum stack by calling MasStk(), then push three values like 5, 15, 10, then call top(), max(), popmax(), max() pop(), top() functions respectively. then the initial stack status will be [5, 15, 10], and corresponding output for ... Read More

Program to find maximum adjacent absolute value sum after single reversal in C++

Arnab Chakraborty
Updated on 26-Dec-2020 11:07:28

177 Views

Suppose we have a list of numbers called nums and we can reverse any sublist in the list at most once. After performing this operation, we have to find the maximum possible value of$\displaystyle\sum\limits_{i=0}^{n-2}| nums[i+1]-[nums[i]|$So, if the input is like nums = [2, 4, 6], then the output will be 6, as when we reverse [4, 6] we will get the list as [2, 6, 4] and the value |2 − 6| + |6 − 4| = 6To solve this, we will follow these steps −if size of nums

Program to construct Frequency Stack in C++

Arnab Chakraborty
Updated on 26-Dec-2020 10:56:34

169 Views

Suppose we want to construct one stack called FrequencyStack, Our FrequencyStack has two functions −append(x), This will append or push a value x onto the stack.pop(), This will remove and returns the most frequent element in the stack. If there are more than one elements with the same frequency, then the element closest to the top of the stack is removed and returned.So, if the input is like append some elements like 7, 9, 7, 9, 6, 7, then perform the pop operations four times, then the output will be 7, 9, 7, 6 respectively.To solve this, we will follow ... Read More

Program to find final amount that should be paid to employees based on their performance in C++

Arnab Chakraborty
Updated on 26-Dec-2020 10:54:40

220 Views

Suppose we have two lists of numbers of same length called performance and costs. And we also have another number k. These indicates that each worker i performs at performance[i] level and it takes costs at least costs[i]. We have to find the minimum cost to hire k employees given also that the workers will be paid proportionate to their performance compared to other employees in the group.So, if the input is like performance = [5, 3, 2] costs = [100, 5, 4] k = 2, then the output will be 10, as we can select emp1 and emp2. They ... Read More

Program to count number of unique subsequences of a string in C++

Arnab Chakraborty
Updated on 26-Dec-2020 10:54:01

659 Views

Suppose we have a string s, we have to find the number of non-empty unique subsequences of s. If the answer is very large then mod the result by 10^9 + 7.So, if the input is like s = "xxy", then the output will be 5, as there are five subsequences: "x", "xx", "xy", "y" and "xxy".To solve this, we will follow these steps −m := 10^9 + 7n := size of sDefine an array table of size 26res := 0for initialize i := 1, when i

Program to find number of unique subsequences same as target in C++

Arnab Chakraborty
Updated on 25-Dec-2020 06:03:39

151 Views

Suppose we have two lowercase strings s and t, we have to find the number of subsequences of s that are equal to t. If the answer is very large then return result by 10^9 + 7.So, if the input is like s = "abbd" t = "bd", then the output will be 2, as there are two possible subsequences "bd".s[1] concatenate s[3]s[2] concatenate s[3].To solve this, we will follow these steps −m := 10^9 + 7if size of t is same as 0, then −return 0if t is same as s, then −return 1if size of t > size ... Read More

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

103 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

342 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

181 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

223 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

Advertisements