Found 7347 Articles for C++

Find maximum height pyramid from the given array of objects in C++

Arnab Chakraborty
Updated on 03-Jan-2020 10:37:33

577 Views

Suppose we have an array of n objects. Each object has width W[i]. We have to arrange them in a pyramidal way like −Total width of ith is less than (i + 1)thTotal number of objects in the ith is less than (i + 1)thFor example, if the weights are like [40, 100, 20, 30], then output will be 2. So top level is 30, then lower level 20, 40 and 100To solve this, we will use the greedy approach. The idea is to use place the objects with lower width at the top, the next object at the level ... Read More

Find Maximum dot product of two arrays with insertion of 0's in C++

Arnab Chakraborty
Updated on 03-Jan-2020 10:34:38

271 Views

Suppose we have two arrays of positive integers of size m and n. The m > n. We have to maximize the dot product by inserting zeros in the second array. One thing we have to keep in mind that we will not change the ordering of the elements in the given arrays. Suppose the arrays are A = [2, 3, 1, 7, 8], and another array B = [3, 6, 7]. The output will be 107. We can maximize the dot product after inserting 0s at first and third position of the second array. So the product will be ... Read More

Find intersection point of lines inside a section in C++

Arnab Chakraborty
Updated on 03-Jan-2020 10:22:06

110 Views

Suppose we have set of lines in the form y = mx + c. There are sections made by this line and the vertical section. We have to find the intersection point present in the given section or not. Suppose the lines are like −L1 = y = x + 2L2 = y = -x + 7L3 = y = -3L4 = y = 2x - 7And the vertical section is given from x = 2 to x = 4.Here intersection points of L1 and L2 are present inside this section, so the answer will be true.To solve this problem, ... Read More

Print all valid words that are possible using Characters of Array in C++

sudhir sharma
Updated on 03-Jan-2020 10:13:33

159 Views

Ib this problem, we are given a set of words and an array of character and we have to check if the words are possible using the characters of the array.Let’s take an example to understand the problem better −Input : words[] : {‘go’ , ‘hi’ , ‘run’ , ‘on’ , ‘hog’ , ‘gone’}    Char[] : {‘a’ , ‘o’ , ‘h’ , ‘g’} Output : go , hog.Explanation − Out of the words, the words that contain the given characters are - go, hog and rest don’t include characters in the char array.To solve this problem, we will use ... Read More

Find if there is a rectangle in binary matrix with corners as 1 in C++

Arnab Chakraborty
Updated on 03-Jan-2020 10:16:32

150 Views

Suppose we have a binary matrix. We have to find if there is any rectangle or sequence in the given matrix whose all four corners are equal to 1. The matrix is like10010001010001010101The result will be yes. Here one rectangle is present, whose corners are with 1s.101010101To solve this we will use one efficient approach. We will follow these steps −Scan the matrix from top to bottom line by lineFor each line remember each combination of two 1’s and push that into a hash-set.If we ever find that combination again in the later line, we will get our rectangle.Example Live ... Read More

Print all ways to break a string in bracket form in C++

sudhir sharma
Updated on 03-Jan-2020 10:10:23

105 Views

In this problem, we are given a string and we have to break it into substrings and print them enclosing brackets.Let’s take a few examples to understand the problem better, Input : wxyz Output :    (w) (x) (y) (z)    (w) (x) (yz)    (w) (xy) (z)    (w) (xyz)    (wx) (y) (z)    (wx) (yz)    (wxy) (z)    (wxyz)Explanation − We will break the string into all possible substrings. And enclose each substring with brackets.Now, since we have understood the problem, let’s create a solution to the problem.Here, we will use recursion to solve the problem. ... Read More

Print all words matching a pattern in CamelCase Notation Dictionary in C++

sudhir sharma
Updated on 03-Jan-2020 10:07:39

249 Views

In this problem, we are given an array of string in camelcase and a pattern. We have to print all those string of the array that match the given pattern.The array of string is an array in which elements are of the string data type.camelCase is a common method for naming in programming, in this way the first letter of the new word starts with an uppercase, rest all are lower case.Example − iLoveProgrammingProblem − find all strings that match a given pattern.Example −Input : “TutorialsPoint” , “ProgrammersPoint” , “ProgrammingLover” , “Tutorials”. Pattern : ‘P’ Output : “TutorialsPoint” , “ProgrammersPoint” ... Read More

Print alternate nodes of a linked list using recursion in C++

sudhir sharma
Updated on 03-Jan-2020 10:03:30

143 Views

A linked list is a linear data structure that stores the element in non-contiguous memory locations. Every element contains a pointer to the next element of the linked list.Example −In this problem, we are given a linked list and we need to print the elements of this linked list but only alternate elements are to be printed. Let’s take an example to understand the problem better, Input : 2 -> 4 -> 1 -> 67 -> 48 -> 90 Output : 2 -> 1 -> 48Explanation − We will print alternate elements on the linked list. So first, third and ... Read More

Find four factors of N with maximum product and sum equal to N in C++

Arnab Chakraborty
Updated on 03-Jan-2020 10:05:31

85 Views

Suppose we have an integer N. The task is to find all factors of N and display the product of four factors of N, such that −Sum of their four factors are equal to NThe product of four factors is maximumSuppose the number is 24, then the product is 1296. As we know all of the factors are 1, 2, 3, 4, 6, 8, 12, 24. We have to choose the factors 6 four times. So 6 + 6 + 6 + 6 = 24. Here the product is maximum.To solve this, we have to find all factors from 1 ... Read More

Print an N x M matrix such that each row and column has all the vowels in it in C++

sudhir sharma
Updated on 03-Jan-2020 10:00:21

226 Views

In this problem, we have to create a 2D matrix of size n X m. And in this matrix, we have to place only vowels in such a way that each row and column has all vowels in it.All vowels mean all a, e, i, o, u are present in each row and each column of the matrix. This makes the minimum number of rows and columns required is 5 i.e. the smallest matrix is of the size 5X5.Let’s take an example to understand the topic betterExample 1 −Input : N = 5 and M = 5. Output :    a ... Read More

Advertisements