Found 7197 Articles for C++

Finding LCM of more than two (or array) numbers without using GCD in C++

Arnab Chakraborty
Updated on 21-Oct-2019 13:52:20

1K+ Views

We have an array A, we have to find the LCM of all elements without using the GCD operation. If the array is like {4, 6, 12, 24, 30}, then the LCM will be 120.The LCM can be calculated easily for two numbers. We have to follow this algorithm to get the LCM.getLCM(a, b) −begin    if a > b, then m := a, otherwise m := b       while true do          if m is divisible by both a and b, then return m             m := m + ... Read More

Finding a Non Transitive Coprime Triplet in a Range in C++

Arnab Chakraborty
Updated on 21-Oct-2019 13:49:21

141 Views

Suppose we have the lower and upper bound, and we have to find nontransitive triplet (x, y, z), such that the pair (x, y) are coprime (GCD is 1), the pair (y, z) are coprime, but pair (x, z) is not a coprime pair. For example, if the lower bound is 2, and upper bound is 10, then the elements are {2, 3, 4, 5, 6, 7, 8, 9, 10}, here possible triplet is (4, 7, 8), here (4, 7), and (7, 8) are coprime, but (4, 8) is not a coprime pair.We will follow the naïve approach to solve ... Read More

Find the Rotation Count in Rotated Sorted array in C++

Arnab Chakraborty
Updated on 21-Oct-2019 13:47:00

271 Views

Consider we have an array, which is rotated sorted array. We have to find number of rotations are required to sort the array. (We will consider rotation right to left.)Suppose the array is like: {15, 17, 1, 2, 6, 11}, then we have to rotate the array two times to sort. The final order will be {1, 2, 6, 11, 15, 17}. Here output is 2.The logic is simple. If we notice, we can see that the number of rotation is same as the value of index of minimum element. So if we get the minimum element, then its index ... Read More

Find the GCD of N Fibonacci Numbers with given Indices in C++

Arnab Chakraborty
Updated on 21-Oct-2019 13:42:27

224 Views

Here we have to find the GCD of n Fibonacci terms with the given indices. So at first we have to get the maximum index, and generate Fibonacci terms, some Fibonacci terms are like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ….. The index is starts from 0. So the element at 0th index is 0. If we have to find gcd of Fibonacci terms at indices {2, 3, 4, 5}, then the terms are {1, 2, 3, 4}, so GCD of these numbers are 1.We will use one interesting approach to do this task. To ... Read More

Find pair with maximum GCD in an array in C++

Arnab Chakraborty
Updated on 21-Oct-2019 13:40:07

651 Views

Suppose we have an array of positive integers. Our task is to find pair of integers from the array, where the GCD value is maximum. Let A = {1, 2, 3, 4, 5}, then the output is 2. The pair (2, 4) has GCD 2, other GCD values are less than 2.To solve this problem, we will maintain a count array to store the count of divisors of each element. The process of counting divisors will take O(sqrt(arr[i])) amount of time. After whole traversal, we can traverse the count array from last index to first index, then if we find ... Read More

Find Count of Single Valued Subtrees in C++

Arnab Chakraborty
Updated on 21-Oct-2019 13:37:05

443 Views

Suppose we have a binary tree. Our task is to count single valued subtrees in the given tree. A single valued subtree is a subtree, where all nodes of that tree is containing same value. Suppose a tree is like below −There are four Single value subtrees. These are like below −We can solve this using bottom up manner. For every subtree visited, return true, if the subtree, rooted under it is single valued, and increase the counter by 1. Here count is the reference parameter for recursive call. And use the returned value to find out if left and ... Read More

Find Corners of Rectangle using mid points in C++

Arnab Chakraborty
Updated on 21-Oct-2019 13:33:40

177 Views

Suppose we have a rectangle ABCD, but we have only the coordinates of the mid points P and Q, and the length of the rectangle L.Our task is to find the coordinates of A, B, C and D using the coordinates of P and Q, and the length of side L. For example, if P is (1, 0), and Q is (1, 2), and L is 2, then A, B, C, D will be respectively (0, 0), (0, 2), (2, 2). (2, 0).There can be three cases that can occur.The rectangle is horizontal, so AD and BC are parallel to ... Read More

Minimum Index Sum for Common Elements of Two Lists in C++

Arnab Chakraborty
Updated on 21-Oct-2019 13:29:49

200 Views

Suppose two person wants to choose different cities, they have listed out the cities in different list, we have to help the, to find common choices. So we need to find those cities, those are marked by both of them.This operation is very similar to the set intersection property, we will take two lists as set, then perform the set intersection to get the common elements.Example Live Demo#include #include #include using namespace std; vector commonInterest(string set1[], int n1, string set2[], int n2) {    vector v(min(n1, n2));    vector::iterator it;    // Sorting both the list    sort(set1, ... Read More

Minimum Cost To Make Two Strings Identical in C++

Arnab Chakraborty
Updated on 21-Oct-2019 13:27:13

215 Views

Suppose we have two strings A and B, and another two cost values like CostA, and CostB. We have to find the minimum cost to make A and B identical. We can delete characters from string, the cost for deleting from string A is CostA, and cost for deleting from string B is CostB. Cost of removing all characters from a string is same. Suppose the string A = “wxyz”, B = “wyzx”, CostA is 10 and CostB is 20. So the output will be 30. If we delete x from both the strings, then A and B will be ... Read More

Minimum Cost to make two Numeric Strings Identical in C++

Arnab Chakraborty
Updated on 21-Oct-2019 13:25:44

164 Views

Suppose we have two numeric strings A and B. We have to find the minimum cost to make A and B identical. We can perform only one operation, that is we can delete digits from string, the cost for deleting a digit from number is same as the digit value. Suppose the string A = “6789”, B = “7859”, Then we have to delete 6 from A, and delete 5 from B, so the cost will be 5 + 6 = 11.This is one of the variation of Longest Common Subsequence problem. We have to find the length of LCS ... Read More

Advertisements