Suppose we have different k sorted arrays. We have to merge these arrays and display the sorted result.So, if the input is like k = 3 and arrays are {2, 4}, {3, 5, 7}, {1, 10, 11, 12} , then the output will be 1 2 3 4 5 7 10 11 12To solve this, we will follow these steps −define one type of pair with first element is an integer and second element is another pair of integers, name it as ppi.Define an array opdefine one priority queue qfor initialize i := 0, when i < size of arr, ... Read More
Suppose we have a string, s, and we have another list with few words, these words are of same length. We have to find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.So if the input is like “wordgoodgoodgoodword” and words are ["word", "good"], then the output will be [0, 12]. This is because the substring starting at index 0 and 12 are “wordgood” and “goodword”.To solve this, we will follow these steps −Define a method called ok(), this will take string s, map wordCnt and ... Read More
Suppose we have a binary tree; we have to calculate the length of the longest path which consists of nodes with consecutive values in increasing order. Every node will be treated as a path of length 1.So, if the input is likethen the output will be 3 as (11, 12, 13) is maximum consecutive path.To solve this, we will follow these steps −Define a function solve(), this will take root, prev_data, prev_length, if not root is non-zero, then −return prev_lengthcur_data := val of rootif cur_data is same as prev_data + 1, then −return maximum of solve(left of root, cur_data, prev_length+1) ... Read More
Suppose we have two strings s1 and s2, we have to find the smallest substring in s1 such that all characters of s2 will be used efficiently.So, if the input is like s1 = "I am a student", s2 = "mdn", then the output will be "m a studen"To solve this, we will follow these steps −N := 26str_len := size of main_str, patt_len := size of patternif str_len < patt_len, thenreturn Nonehash_pat := an array of size N and fill with 0hash_str := an array of size N and fill with 0for i in range 0 to patt_len, dohash_pat[ASCII ... Read More
we have an array A. A has all elements occurring m times, but one element occurs only once. We have to find that unique element.So, if the input is like A = [6, 2, 7, 2, 2, 6, 6], m = 3, then the output will be 7.To solve this, we will follow these steps −INT_SIZE := 8 * size of an integer type variableDefine an array count of size: INT_SIZE. and fill with 0for initialize i := 0, when i < INT_SIZE, update (increase i by 1), do −for initialize j := 0, when j < size, update (increase ... Read More
Suppose we have a sorted array of positive numbers, this array is sorted in ascending order, er have to find the smallest positive value that cannot be represented as sum of elements of any subset of given set. We have to solve this problem in O(n) time.So, if the input is like A = [1, 4, 8, 12, 13, 17], then the output will be 2.To solve this, we will follow these steps −n := size of Aanswer := 1for i in range 0 to n, doif A[i]
Suppose we have a given weighted undirected graph with N different nodes and M edges, some of the nodes are good nodes. We have to find the shortest distance between any pair of two different good nodes. In the given diagram the yellow in the following graph are considered to be good nodes.So, if the input is likethen the output will be 11, as the pairs of good nodes and distance between them are: (1 to 3) the distance is 11, (3 to 5) the distance is 13, (1 to 5) the distance is 24, out of which 11 is ... Read More
Suppose we have a number N this represents the initial position of the person on the number line. We also have L which is the probability of the person of going left. We have to find the the probability of reaching all points on the number line after completing N moves from point N. Each move can be either to the left or to the right.So, if the input is like n = 2, l = 0.5, then the output will be [0.25, 0, 0.5, 0, 0.25]To solve this, we will follow these steps −high := 1 - lowDefine an ... Read More
Suppose we have a binary tree. As we know the succinct encoding of Binary Tree performs close to lowest possible space. The n’th Catalan number is designated by the number of structurally different binary trees with n different nodes. If the n is large, this is about 4n; thus, we require minimum about log2(4) n = 2n bits to encode it. A succinct binary tree therefore would consume 2n + O(n) bits.So, if the input is likethen the output will beencoded −Structure List 1 1 1 0 0 1 0 0 1 0 1 0 0Data List 10 20 40 ... Read More
Suppose we have a Markov chain graph g; we have the find the probability to reach the state F at time T if we start from state S when time t = 0. As we know a Markov chain is a random process consisting of various states and the probabilities to move one state to another. This can be represented as a directed graph; the nodes are states and the edges have the probability of going from one node to another. From one state to another, it takes unit time to move. The sum of the probabilities of the outgoing ... Read More