ConceptWith respect of a given array of 0s and 1s, determine the position of 0 to be replaced with 1 to get maximum continuous sequence of 1s. In this case, expected time complexity is O(n) and auxiliary space is O(1).Inputarr[] = {1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1}OutputIndex 10Let array index starts from 0, replacing 0 with 1 atindex 10 causes the longest continuous sequence of 1s.Inputarr[] = {1, 1, 1, 1, 1, 0}OutputIndex 5MethodUsing count of ones on both sides of zero −Now, the concept is to count number of ones on both ... Read More
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
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
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
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
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
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
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
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
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