Found 7347 Articles for C++

Find the starting indices of the substrings in string (S) which is made by concatenating all words from a list(L) in C++

Arnab Chakraborty
Updated on 27-Aug-2020 12:11:48

129 Views

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

Find the shortest distance between any pair of two different good nodes in C++

Arnab Chakraborty
Updated on 27-Aug-2020 12:03:08

378 Views

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

Find the probability of reaching all points after N moves from point N in C++

Arnab Chakraborty
Updated on 27-Aug-2020 11:58:22

72 Views

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

Find the minimum number of rectangles left after inserting one into another in C++

Arnab Chakraborty
Updated on 20-Aug-2020 08:28:52

138 Views

Suppose we have width and height of N different rectangles; we have to find the minimum number of rectangles left after inserting one into another. So, if W1 and W2 be the width of rectangles R1 and R2 respectively. And H1 and H2 be the height of R1 and R2 respectively, then if W1 < W2 and H1 < H2 then rectangle R1 fits inside rectangle R2. Thus, the smallest one can fit into second smallest, then that fits into the next and so on.So, if the input is like {{ 30, 45 }, { 15, 15 }, { 45, ... Read More

Find the maximum cost of an array of pairs choosing at most K pairs in C++

Arnab Chakraborty
Updated on 20-Aug-2020 07:53:46

147 Views

Suppose we have an array of pairs A; we have to find the maximum cost for selecting at most K pairs. In this case, the cost of an array of pairs type elements is the product of the sum of first elements of the selected pair and the smallest among the second elements of the selected pairs. As an example, if these pairs are selected (4, 8), (10, 3) and (3, 6), then the cost will be (4+10+3)*(3) = 51, for K=3So, if the input is like A = [(15, 5), (65, 25), (35, 20), (20, 5), (35, 20), (15, ... Read More

Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array in C++

Arnab Chakraborty
Updated on 20-Aug-2020 07:42:47

328 Views

Suppose we have two arrays A and B with n numbers, we have to rearrange the elements of B in itself in a way such that the sequence formed by (A[i] + B[i]) % n after rearranging it is lexicographically smallest. Finally we will return the lexicographically smallest possible sequence.So, if the input is like A = {1, 2, 3, 2}, B = {4, 3, 2, 2}, then the output will be [0, 0, 1, 2]To solve this, we will follow these steps −n := size of aDefine one map my_mapDefine one set my_setfor initialize i := 0, when i ... Read More

Find the largest multiple of 3 from array of digits - Set 2 in C++

Arnab Chakraborty
Updated on 20-Aug-2020 07:28:27

103 Views

Suppose we have an array of different digits; we have to find the largest multiple of 3 that can be generated by concatenating some of the given digits in that array in any order. The answer may be very large so make it as string. If there is no answer return an empty string.So, if the input is like [7, 2, 8], then the output will be 87.To solve this, we will follow these steps −Define one 2D array d, there will be three rowssort the array digitssum := 0for initialize i := 0, when i < size of digits, ... Read More

Find the largest area rectangular sub-matrix whose sum is equal to k in C++

Arnab Chakraborty
Updated on 20-Aug-2020 07:25:23

184 Views

Suppose we have a 2D matrix mat and a value K, we have to find the longest rectangular submatrix whose sum is same as K.So, if the input is like28-56-778-311-1443-43110And K = 9then the output will be Top-Left point is (1, 0) and Bottom-Right point is (3, 2).-77811-144-431To solve this, we will follow these steps −MAX := 100Define a function sum_k(), this will take one array arr, start, end, n, k, Define one mapsum := 0, maximum_length := 0for initialize i := 0, when i < n, update (increase i by 1), do −sum := sum + arr[i]if sum is ... Read More

Find the element that appears once in an array where every other element appears twice in C++

Arnab Chakraborty
Updated on 19-Aug-2020 11:49:35

278 Views

Suppose we have an array A. In this array there are different numbers that occurs twice. But there is only one number that occurs once. We have to find that element from that array.Suppose A = [1, 1, 5, 3, 2, 5, 2], then the output will be 3. As there are each number twice, we can perform XOR to cancel out that element. because we know y XOR y = 0To solve this, we will follow these steps.Take one variable res = 0for each element e in array A, preform res := res XOR ereturn resExample Let us see the ... Read More

Find the element having different frequency than other array elements in C++

Arnab Chakraborty
Updated on 19-Aug-2020 11:48:28

139 Views

Suppose we have an array of N numbers, where each element in the array appears same number of times (m times, this is also given) except one element, We have to find this 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 ... Read More

Advertisements