C++ Articles

Page 257 of 597

Find Harmonic mean using Arithmetic mean and Geometric mean using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 415 Views

Here we will see how to get the Harmonic mean using the arithmetic mean and the geometric mean. The formula for these three means are like below −Arithmetic Mean − (a + b)/2Geometric Mean − $$\sqrt{\lgroup a*b\rgroup}$$Harmonic Mean − 2ab/(a+b)The Harmonic Mean can be expressed using arithmetic mean and geometric mean using this formula −$$HM=\frac{GM^{2}}{AM}$$Example#include #include using namespace std; double getHarmonicMean(int a, int b) {    double AM, GM, HM;    AM = (a + b) / 2;    GM = sqrt(a * b);    HM = (GM * GM) / AM;    return HM; } int main() ...

Read More

Find if a number is part of AP whose first element and difference are given using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 571 Views

Suppose we have the first element of AP, and the differenced. We have to check whether the given number n is a part of AP or not. If the first term is a = 1, differenced = 3, and the term x = 7 will be checked. The answer is yes.To solve this problem, we will follow these steps −If d is 0, and a = x, then return true, otherwise false.Otherwise, if d is not 0, then if x belongs to the sequence x = a + n * d, where n is a non-negative integer, only if (n ...

Read More

Find if two rectangles overlap using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 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

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 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#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

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 467 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 N Arithmetic Means between A and B using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 208 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 N Geometric Means between A and B using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 270 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 number of magical pairs of string of length L in C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 245 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 one extra character in a string using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 507 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#include #include using namespace std; char getExtraCharacter(string S, string T) {    unordered_map char_map;   ...

Read More

Find original array from encrypted array (An array of sums of other elements) using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 671 Views

Let us consider we have an array of integers, that array is encrypted array, Suppose the array is A = [10, 14, 12, 13, 11], the original array is B = [5, 1, 3, 2, 4], we can see that each element at index I of A follows this rule: A[i] = sum of all elements at position j in B[j], where I ≠ j. Our task is to find an original array from the encrypted one.The task is based on arithmetic observation. Suppose the array is of size 4, original array B has four elements B = [a, b, ...

Read More
Showing 2561–2570 of 5,962 articles
« Prev 1 255 256 257 258 259 597 Next »
Advertisements