Server Side Programming Articles - Page 1688 of 2646

Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated in C++

Arnab Chakraborty
Updated on 24-Jul-2020 11:33:42

191 Views

ConceptWith respect of given two strings S1 and S2 of equal lengths, our task is to determine an index i such that S1[0…i] and S2[i+1…n-1] give a palindrome when concatenated together. Ithas been seen that if it is not possible to determine such an index then print -1.InputS1 = “pqrsu”, S2 = “wxyqp”Output1S1[0..1] = “pq”, S2[2..n-1] = “ypq”S1 + S2 = “pqyqp” indicates is a palindrome.InputS1 = “pqrst”, S2 = “qprqz”Output-1MethodAt first, we iterate from 0 to n (length of the string) and copy ith character from S1 to another string (assume it is S).After that we take another temporary ... Read More

Find if there is a triplet in a Balanced BST that adds to zero in C++

Arnab Chakraborty
Updated on 25-Aug-2020 12:33:38

248 Views

Suppose we have a balanced binary search tree, we have to create a function named is_valid_triplet() that returns true when there exist a triplet in given BST whose sum equals to 0, otherwise returns false. Design the method by following these constraints −expected time complexity is O(n^2)O(logn) extra space can be used.So, if the input is likethen the output will be True, as triplet is [-15, 7, 8]To solve this, we will follow these steps −Define a function bst_to_doubli_list(), this will take root, head, tail, if root is same as NULL, then −returnif left of root is not null, then ... Read More

Find if there is a path of more than k length from a source in C++

Arnab Chakraborty
Updated on 24-Jul-2020 11:28:25

199 Views

ConceptWith respect of a given graph, a source vertex in the graph and a number k(here k indicates the path length of graph between source vertex and destination vertex), our task is to determine if there is a simple path (without any cycle) beginning from given source and ending at any other vertex(i.e. destination). The graph is shown in following −InputSource s = 0, k = 64OutputTrueThere exists a simple path 0 -> 7 -> 1-> 2 -> 8 -> 6 -> 5 -> 3 -> 4, which has a total distance of 68 km which is more than 64.InputSource ... Read More

Find if neat arrangement of cups and shelves can be made in C++

Arnab Chakraborty
Updated on 24-Jul-2020 11:23:06

162 Views

ConceptWith respect of given three different types of cups (p[]) and saucers (q[]), and m numberof shelves, determine if neat arrangement of cups and shelves can be made.Now, arrangement of the cups and saucers will be neat if it follows the following rules −According to first rule, no shelf can contain both cups and saucers.According to second rule, there can be no more than 5 cups in any shelf.According to third rule, there can be no more than 10 saucers in any shelf.Inputp[] = {4, 3, 7} q[] = {5, 9, 10} m = 11OutputYesExplanationTotal cups = 14, shelves required ... Read More

Find if it is possible to get a ratio from given ranges of costs and quantities in C++

Arnab Chakraborty
Updated on 24-Jul-2020 11:22:07

126 Views

ConceptWith respect of the given range of cost from lowCost to upCost and range of quantity from lowQuant to upQuant, determine if it is possible to obtain a given ratio r where r=cost/quantity, and lowCost

Find if given vertical level of binary tree is sorted or not in C++

Arnab Chakraborty
Updated on 24-Jul-2020 11:20:26

160 Views

ConceptWith respect of a given binary tree, our task is to determine if a given vertical level of the binary tree is sorted or not.(With respect of this case, when two nodes are overlapping, verify if they form a sorted sequence in the level they lie.)Input2 / \ 3 6 / \ 8 5   / 7 Level l = -1OutputYesNodes in level -1 are 3 -> 7 which form a sorted sequence.Input2 / \ 3 7 \ / 4 5 Level l = 0OutputYesIt should be noted that nodes with value 4 and 5 are overlapping in the binary ... Read More

Find if an undirected graph contains an independent set of a given size in C++

Arnab Chakraborty
Updated on 24-Jul-2020 11:17:08

191 Views

ConceptWith respect of a given undirected graph, verify if it contains an independent set of size l. If there exists an independent set of size l print ‘Yes’, else print ‘No’. It should be noted that an independent set in a graph is defined as a set of vertices which are not directly connected to each other.InputL = 4, graph = [[1, 0, 1, 0, 0], [0, 1, 1, 0, 0], [1, 1, 1, 1, 1], [0, 0, 1, 1, 0], [0, 0, 1, 0, 1]];OutputYesThe above graph contains an independent set of size 4 (vertices 0, 1, 3, 4 ... Read More

Find Four points such that they form a square whose sides are parallel to x and y axes in C++

Arnab Chakraborty
Updated on 24-Jul-2020 11:11:28

1K+ Views

ConceptWith respect of given ‘n’ pair of points, our task is to determine four points so that they form a square whose sides are parallel to x and y axes or else display “No such square”. It should be noted that if more than one square is possible then select the one with the maximum area.Inputn = 6, points = (2, 2), (5, 5), (4, 5), (5, 4), (2, 5), (5, 2)OutputSide of the square is: 3, points of the square are 2, 2 5, 2 2, 5 5, 5ExplanationThe points 2, 2 5, 2 2, 5 5, 5 form ... Read More

Find four missing numbers in an array containing elements from 1 to N in C++

Arnab Chakraborty
Updated on 24-Jul-2020 11:08:21

195 Views

ConceptWith respect of a given array of unique integers where each integer of the given array lies in the range [1, N], the size of array is (N-4) and no single element is repeated. So, four numbers, from 1 to N, are missing in the array. Determine the 4 missing numbers in sorted order.Inputarr[] = {3, 6, 7, 4, 10}Output1 2 5 8 9Inputarr[] = { 2, 8, 4, 13, 6, 11, 9, 5, 10 }Output1 3 7 12MethodNow a simple O(N) solution is to implement an auxiliary array of size N to indicate or markvisited elements. Visit the input ... Read More

C++ find four factors of N with maximum product and sum equal to N .

Arnab Chakraborty
Updated on 24-Jul-2020 11:06:49

169 Views

ConceptWith respect of a given integer N, our task is to determine all factors of N print the product of four factors of N so that −The sum of the four factors is equal to N.The product of the four factors is largest.It has been seen that if it is impossible to find 4 such factors then print “Not possible”.It should be noted that all the four factors can be equal to each other to maximize the product.Input24OutputAll the factors are -> 1 2 4 5 8 10 16 20 40 80 Product is -> 160000Select the factor 20 four ... Read More

Advertisements