Found 7346 Articles for C++

C++ program to Reorder the Position of the Words in Alphabetical Order

Prateek Jangid
Updated on 10-Aug-2022 08:48:38

442 Views

In this problem, a string is taken as the input and we have to sort the words present in the string in a lexicographical order. To do that, we assign an index to each word in the string (differentiated with spaces in between) starting from 1, and the output is obtained in the form of sorted index. String = {“Hello”, “World”} “Hello” = 1 “World” = 2 Since the words in the input string are already in lexicographical order, the output is printed as “1 2”. Let us look at some input/result scenarios − Assuming all the words in ... Read More

C++ program to Reorder the Given String to Form a K-Concatenated String

Prateek Jangid
Updated on 10-Aug-2022 13:03:21

88 Views

We are given a string and an integer k, and we need to reorder the characters in the string so that it becomes the concatenation of k similar substring. If not possible, output the result as "Impossible”. string = "malaalam"; K = 2; res = solve(s, K); Example (Using Maps) Let's have a string "mottom" and K=2. The given string can be represented as the concatenation of 2 substrings like tomtom, motmot omtomt, etc. As in all 3, two substrings are joined together when k = 2. Using the string, we can determine how many times each character occurs. ... Read More

C++ program to remove spaces from a string using String stream

Prateek Jangid
Updated on 10-Aug-2022 08:41:07

720 Views

As the given problem says, we need to remove spaces from the string using a string stream. As the name suggests, a string stream converts a string into a stream. It works similar to cin in C++. It associates a string object that can access the string buffer in which it is stored. string s =" a for apple, b for ball"; res = solve(s); With a string buffer, we will read each word one by one and then concatenate it into a new string which will be our answer. Note − The class string stream is available ... Read More

C++ program to remove row or column wise duplicates from matrix of characters

Prateek Jangid
Updated on 10-Aug-2022 12:18:34

386 Views

We are given a 2D matrix with rows and columns. The matrix consists of elements in char data type. A method is devised to remove the elements which are duplicated in their respective rows or columns. In this method, we check if any element is repeating in its row or column for each character. If it is not repeated, we leave it as it was before. We can store the values occurring in each row and column in a map. After which, we can traverse again and take those values which are appearing only once in their row and column. ... Read More

Removing a Number from Array to make It Geometric Progression using C++

Prateek Jangid
Updated on 10-Aug-2022 08:29:25

100 Views

We are given an array of elements. We need to find whether the elements in the array are in Geometric Progression (GP) or not after removing any 1 element from the array. We can run out the possibilities and with observations to figure out that the first element is fake, or the second element is fake, or these 2 elements will give the common ratio of the array. After the common ratio is found, we can iterate on the array to see if all elements follow that rule. 2 base conditions would be to check if the first and second ... Read More

C++ program to remove minimum elements from either side such that 2*min becomes more than max

Prateek Jangid
Updated on 10-Aug-2022 08:27:13

266 Views

The problem involves removing elements from any side of a list of integers in such a way that 2*min is greater than max. vector arr = {250, 10, 11, 12, 19, 200}; res = solve(arr); We can use brute force approach. We can try all possible subarrays which satisfy and find the longest one in which 2*min > max condition holds. We can also use dynamic programming approach to try all possible subarray combinations that are overkill and not required. Example  (Using Vector ADT) Suppose we have an array such as “[250, 10, 11, 12, 19, 200]”. To get ... Read More

Remove Duplicates from a Sorted Linked List Using Recursion

Prateek Jangid
Updated on 10-Aug-2022 07:53:32

425 Views

A linked list is a sequence of elements that are connected together. Each list have a head and series of nodes and each node has data of the current node and link to next node. The basic operations of a linked list are insertion, deletion, search and delete. Removing duplicates from a sorted linked list One way to remove nodes from is using recursion. The idea is to compare each node with its adjacent node and delete the duplicate one they are equal. Our recursive call will return us to the next node. So for the next element, we will ... Read More

C++ program to remove characters from a numeric string to make it divisible by 8

Prateek Jangid
Updated on 10-Aug-2022 07:50:42

111 Views

Given a number in the form of a string, we need to find where to make it divisible by eight after deleting zero or more elements. In other words, we need to find whether there is a subsequence of the string, which is divisible by 8. Return the modified string or -1 if it is not possible. According to divisibility rules, any number whose last three digits are divisible by 8 is also divisible by 8. For example, 56992992 and 476360 are divisible by 8, but 2587788 is not. If the result is a whole number, then the original number ... Read More

Removing Brackets from an Algebraic String Containing + and – Operators using C++

Prateek Jangid
Updated on 10-Aug-2022 07:47:23

412 Views

Given an algebraic string like p-(q-r)-s, we need to remove the brackets and convert this string to a string with the same mathematical result. So the string p-(q-r)-s is converted to p-q+r-s, giving us the same mathematical result. To achieve this, we can use a stack and keep track of whether or not we should flip the upcoming signs in the bracket expression. 0 mean + or no flip 1 mean - or flip So on every bracket opening, we will push either 0 or 1 depending on whether the signs in this bracket will flip or not. ... Read More

C++ program to remove Nodes that Don't Lie in Any Path With Sum>=k

Prateek Jangid
Updated on 10-Aug-2022 07:27:18

111 Views

In this problem, we have a binary tree with a path from the root node to the leaf node that is completely defined. The sum of all nodes from the root node to the leaf node must be greater than or equal to, a constant value, k. So we need to remove all nodes in those paths whose sum is less than k, such that the only paths left in the tree will be greater than k. An important thing to remember here is that a node may be a part of many paths, so only remove such nodes if ... Read More

Advertisements