Found 7346 Articles for C++

Smallest integer > 1 which divides every element of the given array: Using C++

Prateek Jangid
Updated on 10-Aug-2022 09:51:06

174 Views

In this article, we are given integers in an array, and we must find the smallest number which is greater than 1 that divides all the elements in the array. For example, let us consider a sample array [30, 90, 15, 45, 165]. vector arr = {30, 90, 15, 45, 165}; result = solve(arr); Now we can find the array's GCD(greatest common divisor). If it comes out to be 1 that means that only 1 can divide the whole array, and we can return -1 or "Not possible." If it's an integer, then this integer divides the whole ... Read More

C++ program to Replace Nodes with Duplicates in Linked List

Prateek Jangid
Updated on 10-Aug-2022 09:49:07

314 Views

In this article, we are given a linked list that contains elements from 1 to n and duplicates. Elements 1 to n will always be there with duplicates from [1..n]. We need to replace every duplicate element with n+1, n+2, and so on. Lets consider an example 1→2→2→4→5→3→6→6 Next n = 42. So every duplicate is replaced with n+1, n+2, and so on. The next 42 is replaced with 47, and the next 46 is replaced with 48, leaving the first instance as it is. First of all, we need to construct a binary tree in the main method as ... Read More

C++ program to Replace Every Matrix Element with Maximum of GCD of Row or Column

Prateek Jangid
Updated on 10-Aug-2022 09:47:24

354 Views

In this method, we need to replace every element in the given matrix with the maximum Greatest Common Divisor (GCD) of that row and column. Let us look at some input scenarios − Suppose we are given a 2D matrix of dimensions m*n is; Input: [[3, 2, 1, 4] [7, 6, 2, 8] [14, 20, 25, 17]]; In the above matrix, row 1 gcd(3, 2, 1, 4) = 1 and Column 2 gcd(3, 7, 14) = 1. So element 2 (1st row and 2nd column) becomes the maximum (1, 1) = 1. So on for all elements and our ... Read More

Replace every element with the least greater element on its right using C++

Prateek Jangid
Updated on 10-Aug-2022 09:27:51

157 Views

To find the least greater element on the right of an array of integers, we need to consider an array of integers. For each element, we need to find an element that is greater than the current element and is the least from all the candidate elements we have. Suppose we have elements such as [5, 23, 65, 31, 76, 32, 87, 23, 76, 32, 88]. vector arr = {5, 23, 65, 31, 76, 32, 87, 23, 76, 32, 88}; solve(arr); We can list our requirements and then think over the requirements to find a solution. We need a ... Read More

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

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

129 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

231 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

403 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

978 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

321 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

190 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

Advertisements