Suppose we have a 2D list of values called 'tree' which represents an n-ary tree and another list of values called 'color'. The tree is represented as an adjacency list and its root is tree[0].The characteristics of an i-th node −tree[i] is its children and parent.color[i] is its color.We call a node N "special" if every node in the subtree whose root is at N has a unique color. So we have this tree, we have to find out the number of special nodes.So, if the input is like tree = [ [1, 2], [0], [0, 3], ... Read More
Suppose we have a list of words. We have to check the given words can be chained to form a circle. A word A can be placed in front of another word B in a chained circle if only the last character of A is identical to the first character of B. Every word has to be used and can be used only once (the first/last word will not be considered).So, if the input is like words = ["ant", "dog", "tamarind", "nausea", "gun"], then the output will be True.To solve this, we will follow these steps −graph := a new ... Read More
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
Suppose we have a 2D matrix named 'edges', that represents an undirected graph. Every item in the matrix 'edges' represents an edge and is of the form (u, v, w). This means nodes u and v are connected and the edge has the weight w. We also have integers a and b, that represent an edge (a, b). We have to find out if the edge (a, b) is part of a minimum spanning tree.Note − the graph has to be connected and the edge (a, b) exists in the graph.So, if the input is like edges =[[0, 2, 100], ... Read More
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
Suppose we have a 2D matrix and another value k. Our goal is to return a matrix that contains the lowest values of all k x k sub-matrices.So, if the input is like3568654312and k =2, then the output will be [[3, 5], [3, 3]] .From the input, we can see that the top left submatrix has the lowest value of 33 5 8 6The top right submatrix has the lowest value of 55 6 6 5The bottom left submatrix has the lowest value of 38 6 4 3The bottom right submatrix has the lowest value of 36 5 3 12To ... Read More
Suppose we have a list of numbers called nums, The width of a sequence of numbers as the difference between the maximum and minimum number in the sequence. We have to find the sum of widths of all subsequences of nums. If the answer is very large then mod the result by 10^9+7.So, if the input is like nums = [7, 4, 9], then the output will be 15, as we have the subsequences like: [7], [4], [9], [7, 4], [7, 9], [4, 9], [7, 4, 9] and So the widths are 0, 0, 0, 3, 2, 5, 5, so ... Read More
Suppose we have a list of numbers called nums and we can delete at most one element in the list. We have to find the maximum number of sublists that contain both the maximum and minimum values of the resulting list.So, if the input is like nums = [3, 2, 6, 2, 4, 10], then the output will be 8, as if we remove 10 we will get [3, 2, 6, 2, 4] and there are eight sublists where it contains both the max and the min −[2, 6][6, 2][2, 6, 2][3, 2, 6][6, 2, 4][2, 6, 2, 4][3, 2, ... Read More
Suppose we have a list of numbers called nums and another value pos. We have to find a sublist A of nums that includes the index pos such that (minimum of A) * (size of A) is maximized then return the value.So, if the input is like nums = [-2, 2, 5, 4] pos = 3, then the output will be 8, as the best sublist is [5, 4], because (5, 4) = 4 and its size is 2 we have 4 * 2 = 8.To solve this, we will follow these steps −ans := A[pos], m := A[pos]i := ... Read More
Suppose we have a string s, this is encoding a longer string. s is represented as the concatenation of n(t), n(t) represents the concatenation of t, n times, and t is either a regular string or it is another encoded string recursively. We have to find the decoded version of s.So, if the input is like s = "3(pi)2(3(am))0(f)1(u)", then the output will be "pipipiamamamamamamu"To solve this, we will follow these steps −i := 0Define a function parse() . This will takeans := a new listwhile i < size of s and s[i] is not same as ")", doif s[i] ... Read More