Found 7197 Articles for C++

Replace each node with its Surpasser Count in Linked List Using C++

Prateek Jangid
Updated on 10-Aug-2022 09:16:23

215 Views

We are given a linked list, and we need to find elements in the given linked list which are greater and present in the right of the current element. The count of these elements needs to be substituted into the current node's value. Let's take a linked list with the following characters and replace each node with its Surpasser count − 4 -> 6 -> 1 -> 4 -> 6 -> 8 -> 5 -> 8 -> 3 From backward, traverse the linked list (so we do not need to worry about elements to the left of the current). ... Read More

C++ program to Replace Duplicates with Greater than Previous Duplicate Value

Prateek Jangid
Updated on 10-Aug-2022 12:54:28

320 Views

A series of integers is given in this article. Let's say we have an array of four elements without counting the repetitive elements, [2, 2, 5, 5, 7, 8, 7], and we have to make the array distinctive. Changing a value with one that is greater than the previous one is possible. In the above array, element 2 at index 1 becomes 3 as the next greater element. 5 at index 3 becomes 6 as the next greater and so on. So, in the end, our array becomes [2 3 5 6 7 8 9] and should minimize the sum ... Read More

C++ program to replace all occurrences of string AB with C without using extra space

Prateek Jangid
Updated on 10-Aug-2022 09:01:33

516 Views

In this article, we will replace "AB" with "C" in the given string having upper case latin characters. The occurrences of "AB" becomes "C, " but single "A" and "B" are unaffected. Let us look at some input scenarios − Let's have a string "ABOUTME" Input: "ABOUTME" Result: COUTME We start traversing the string from index one. We then check the current and previous elements for "B" and "A, " respectively. If we find it, then we replace the last append ("A") with a "C." The worst case time complexity will occur when there is no substring “AB” ... Read More

Replace All Consonants with Nearest Vowels In a String using C++ program

Prateek Jangid
Updated on 10-Aug-2022 08:59:19

1K+ Views

The method is devised to replace a string of consonants with the closest vowels from the alphabet (also known as lowercase Latin letters). If two vowels are equally close, we can replace them with the first ones in these letters. Let us look at some input scenarios − Suppose we have a string such as “ebgkjasjd, ” and now we need to change all the present consonants with the nearest vowels in a string. Input = "ebgkjasjd"; Result = ebgkjasjd eaeiiauie Taking element ‘b, ’ we can replace it with ‘a’ since that is the nearest vowel. We could ... Read More

Repeatedly Search for an Element by Doubling it After Every Successful Search Using C++

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

397 Views

In this article, we are given an array of integers and a key. We must find the key repeatedly in the array and double it on each find in the array. We need to return the value not present in the array doing this operation. Some input scenarios to look at to understand how the method works in different cases Let's have an array [1, 2, 6, 3, 7, 4, 9], and its key is 1. Input: {1, 2, 3, 4, 5, 6}, k = 1 Result: 8 If we find 1 we double it to 2. If we ... Read More

C++ Program to find if the given string has Repeated Subsequence of Length 2 or More

Prateek Jangid
Updated on 10-Aug-2022 08:52:17

275 Views

Given a string, find a subsequence with the length, at least two, repeated in the string. The index of the subsequence element numbers must not be in the same order. string s = "PNDPNSP"; print("Repeated subsequence of length 2 or more: ", (check(s) ? "Yes" : "No")); Let us look at few examples below to see how the method works in different cases − Example 1 − str = "PNDPNSP" Explanation − Here, the answer is true because there is a subsequence "PN, " repeated in the string. Example 2 − str = "PPND" Explanation − Here, the answer ... Read More

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

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

548 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

157 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

943 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

518 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

Advertisements