Suppose we have a 2d binary matrix where 0 represents empty cell and 1 represents a wall. We have to find the minimum number cells that need to become walls so that there will be no path between top−left cell and bottom-right cell. We cannot put walls on the top−left cell and the bottom−right cell. We can move only left, right, up and down not diagonally.So, if the input is like0000010001100000then the output will be 2, 0100010001100010To solve this, we will follow these steps −R := row count of matrix, C := column count of matrixvisited := a new settin ... Read More
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
Suppose we have a lowercase string s, we have to find the sum of the count of characters that are distinct in every substring of s. If the answer is very large then return result mod 10^9+7.So, if the input is like s = "xxy", then the output will be 6, as the substrings and their counts are −"x" : 1"x" : 1"y" : 1"xx" : 0 (as "x" is not distinct)"xy" : 2"xxy" : 1 ("as "x" is not distinct)To solve this, we will follow these steps −m := 10^9 + 7prev_seen := a new empty mapans := 0Define ... Read More
Suppose we have a list of numbers called nums. Now let us consider an operation where we take two consecutive values and merge it into one value by taking their sum. We have to find the minimum number of operations required so that the list turns into non−increasing.So, if the input is like nums = [2, 6, 4, 10, 2], then the output will be 2, as we can merge [2, 6] to get [8, 4, 10, 2] and then merge [8, 4] to get [12, 10, 2].To solve this, we will follow these steps −if nums is empty, thenreturn ... Read More
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
Suppose we have a number k, now consider the smallest positive integer value x where all values from 1 to k divide evenly. In other words, consider the smallest value x where x is divisible by all numbers from 1 through k. We have to find the number of trailing zeroes in x.So, if the input is like k = 6, then the output will be 0, as the smallest x here is 60, 60 can be divided using 1, 2, 3, 4, 5 and 6. There is only one trailing zeroes in 60.To solve this, we will follow these steps −res := 0x := 1while x * 5
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
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
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
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