Found 7197 Articles for C++

Find one extra character in a string using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:11:33

434 Views

Suppose we have two strings S and T, the length of S is n, and the length of T is n + 1. The T will hold all characters that are present in S, but it will hold one extra character. Our task is to find the extra character using some efficient approach.To solve this problem, we will take one empty hash table, and insert all characters of the second string, then remove each character from the first string, the remaining character is an extra character.Example Live Demo#include #include using namespace std; char getExtraCharacter(string S, string T) {    unordered_map char_map; ... Read More

Find number of pairs in an array such that their XOR is 0 using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:09:40

181 Views

Suppose we have an array of n elements; we have to find a number of pairs in the array whose XOR will be 0. The pair (x, y) whose XOR is 0, then x = y. To solve it we can sort the array, then if two consecutive elements are the same, increase the count. If all elements are the same, then the last count may not be counted. In that case, we will check whether the last and first elements are the same or not, if the same, then increase the count by 1.Example#include #include using namespace std; int ... Read More

Find number of magical pairs of string of length L in C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:07:35

186 Views

Suppose we have two strings str1 and str2, we have to find a number of magical pairs of length L. Two strings will be magical if for every index I, the str1[i] < str2[i]. We have to count a number of pairs since the number is very large, then return the answer using modulo 109. The strings will hold only lowercase letters.The approach is simple. As we can see, if the length is L = 1, and index i = 1 is holding ‘a’, in str1 then index i = 1 of str2 will hold from ‘b’ to ‘z’ so ... Read More

Find N Geometric Means between A and B using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:04:44

228 Views

Suppose we have three integers A, B and N. We have to find N geometric means between A and B. If A = 2, B = 32, and N = 3, then the output will be 4, 8, 16The task is simple we have to insert N number of elements in the geometric Progression where A and B are the first and last term of that sequence. Suppose G1, G2, …. Gn are n geometric means. So the sequence will be A, G1, G2, …. Gn, B. So B is the (N + 2)th term of the sequence. So we ... Read More

Find N Arithmetic Means between A and B using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:03:19

141 Views

Suppose we have three integers A, B and N. We have to find N arithmetic means between A and B. If A = 20, B = 32, and N = 5, then the output will be 22, 24, 26, 28, 30The task is simple we have to insert N number of elements in the Arithmetic Progression where A and B are the first and last term of that sequence. Suppose A1, A2, …. An are n arithmetic means. So the sequence will be A, A1, A2, …. An, B. So B is the (N + 2)th term of the sequence. ... Read More

Find minimum sum of factors of number using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:01:24

401 Views

Here we will see how to get a minimum sum of factors of a given number. Suppose a number is 12. We can factorize this in different ways −12 = 12 * 1 (12 + 1 = 13)12 = 2 * 6 (2 + 6 = 8)12 = 3 * 4 (3 + 4 = 7)12 = 2 * 2 * 3 (2 + 2 + 3 = 7)The minimum sum is 7. We will take a number, and try to find the minimum factor sum. To get the minimum factor sum, we have to factorize the number as long ... Read More

Find max in struct array using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 05:58:55

705 Views

Here we will see how to get max in the struct array. Suppose there is a struct like below is given. We have to find the max element of an array of that struct type.struct Height{    int feet, inch; };The idea is straight forward. We will traverse the array, and keep track of the max value of array element in inches. Where value is 12*feet + inchExample#include #include using namespace std; struct Height{    int feet, inch; }; int maxHeight(Height h_arr[], int n){    int index = 0;    int height = INT_MIN;    for(int i = 0; i ... Read More

Find last two digits of sum of N factorials using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 05:57:05

185 Views

Here we will see how to get the last two digits. The unit place digit and the tens place digit of the sum of N factorials. So if N = 4, it will be 1! + 2! + 3! + 4! = 33. so unit place is 3 and ten place is 3. The result will be 33.If we see this clearly, then as the factorials of N > 5, the unit place is 0, so after 5, it will not contribute to change the unit place. And after N > 10, the ten places will remain 0. For N ... Read More

Find largest prime factor of a number using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 05:53:55

1K+ Views

Consider we have an element x, we have to find the largest prime factor of x. If the value of x is 6,  then-largest prime factor is 3. To solve this problem, we will just factorize the number by dividing it with the divisor of a number and keep track of the maximum prime factor.Example Live Demo#include #include using namespace std; long long getMaxPrimefactor(long long n) {    long long maxPF = -1;    while (n % 2 == 0) {       maxPF = 2;       n /= 2;    }    for (int i = 3; i 2)    maxPF = n;    return maxPF; } int main() {    long long n = 162378;    cout

Find if two rectangles overlap using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 05:51:34

1K+ Views

We know that a rectangle can be represented using two coordinates, the top left corner, and the bottom right corner. Suppose there are two rectangles, we have to check whether these two overlap or not. There are four coordinate points (l1, r1) and (l2, r2).l1 is the top-left corner of first rectangler1 is the bottom-right corner of the first rectanglel2 is the top-left corner of second rectangler2 is the bottom-right corner of the second rectangleWe have assumed that the rectangles are parallel to the coordinate axes. To solve this, we have to check a few conditions.One rectangle is above the ... Read More

Advertisements