C++ Articles

Page 542 of 597

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Oct-2019 314 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
Arnab Chakraborty
Updated on 21-Oct-2019 257 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 Corners of Rectangle using mid points in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Oct-2019 217 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
Arnab Chakraborty
Updated on 21-Oct-2019 225 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
Arnab Chakraborty
Updated on 21-Oct-2019 250 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

Minimize ASCII values sum after removing all occurrences of one character in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Oct-2019 186 Views

Suppose we have a string. We have to minimize the sum of ASCII values, of each character to the string, after removing every occurrence of a particular character. Suppose a string is given like “hello” the sum of ASCII characters is (104 + 101 + 108 + 108 + 111) = 532. Now check occurrences of each characters.h has occurred one time, so cost is 1 * 104 = 104e has occurred one time, so cost is 1 * 101 = 101l has occurred one time, so cost is 2 * 108 = 216o has occurred one time, so cost ...

Read More

Median and Mode using Counting Sort in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Oct-2019 447 Views

Consider we have an array of size n, we have to find the Median and Mode using the counting sort technique. This technique is useful, when the array elements are in limited range. Suppose the elements are {1, 1, 1, 2, 7, 1}, then the Mode is 1, and Median is 1.5. Let us see what is Median and what is Mode −Median is the middle number in a sorted list of numbersMode is the element whose occurrences are maximum in the listTo get the Median and Mode, we have to follow these steps −Assume that the size of the ...

Read More

Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Oct-2019 266 Views

Suppose we have a complete graph; we have to count number of Edge Disjoint Spanning trees. The Edge Disjoint Spanning trees are spanning trees, where no two trees in the set have an edge in common. Suppose the N (number of vertices) is 4, then output will be 2. The complete graph using 4 vertices is like below −Two edge disjoint spanning trees are like −The maximum number of edge disjoint spanning tree from a complete graph, with N vertices will be $[\frac{n}{2}]$Example#include #include using namespace std; int maxEdgeDisjointSpanningTree(int n){    return floor(n/2); } int main() {    int n = 4;    cout

Read More

Sort elements of the array that occurs in between multiples of K in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Oct-2019 194 Views

Suppose we have an array A, and another integer K. We have to sort the elements that are in between any two multiples of K. Suppose A is like [2, 13, 3, 1, 21, 7, 8, 13, 12], and K = 2. The output will be [2, 1, 3, 7, 13, 21, 8, 13, 12]. Here multiple of 2 are 2, 8 and 12, the elements in between 2 and 8 are 13, 3, 1, 21, 7, they will be sorted as 1, 3, 7, 13, 21, the elements between 8 and 12 is only 13, so that is already ...

Read More

Check if a number is formed by Concatenation of 1, 14 or 144 only in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Oct-2019 254 Views

Here we will see one problem, that can tell that whether a string or a number is a concatenation of 1, 14 or 144 only. Suppose a string is “111411441”, this is valid, but “144414” is not valid.The task is simple, we have to fetch a single digit, double-digit and triple-digit number from the last, and check whether they match with any of these three (1, 14 and 144), if we get one match, divide the number with it, and repeat this process until the entire number is not exhausted.Example#include #include using namespace std; bool checkNumber(long long number) ...

Read More
Showing 5411–5420 of 5,961 articles
« Prev 1 540 541 542 543 544 597 Next »
Advertisements