Server Side Programming Articles - Page 2131 of 2650

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

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

412 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

718 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

195 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

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

Arnab Chakraborty
Updated on 30-Oct-2019 05:47:45

528 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 Harmonic mean using Arithmetic mean and Geometric mean using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 05:44:56

358 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 Live Demo#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 ... Read More

Find floor and ceil in an unsorted array using C++.

Arnab Chakraborty
Updated on 29-Oct-2019 12:03:00

690 Views

Here we will see how to find the floor and ceiling in an unsorted array. The floor value is larger element which is smaller than or equal to x, and the ceiling value is smallest value which is larger than x. If the array A = [5, 6, 8, 9, 6, 5, 5, 6], and x is 7, then the floor value is 6, and the ceiling value is 8.To solve this problem, we will follow the linear search approach. We will traverse the array and track two distances with respect to x.Min distance of element greater than or equal ... Read More

Find difference between sums of two diagonals in C++.

Arnab Chakraborty
Updated on 29-Oct-2019 11:54:13

549 Views

Here we will see how to get the difference between the sums of two diagonals of a given matrix. Suppose we have a matrix of order N x N, we have to get the sum of primary and secondary diagonals, then get the difference of them. To get the major diagonal, we know that the row index and column index increases simultaneously. For the second diagonal, row index and column index values are increased by this formula row_index = n – 1 – col_index. After getting the sum, take the difference and return a result.Example Live Demo#include #include #define MAX 100 ... Read More

Find column with maximum sum in a Matrix using C++.

Arnab Chakraborty
Updated on 29-Oct-2019 11:44:18

491 Views

Suppose we have a matrix of size M x N. We have to find the column, that has a maximum sum. In this program we will not follow some tricky approach, we will traverse the array column-wise, then get the sum of each column, if the sum is the max, then print the sum and the column index.Example#include #define M 5 #define N 5 using namespace std; int colSum(int colIndex, int mat[M][N]){    int sum = 0;    for(int i = 0; i maxSum) {          maxSum = sum;          index = i;       }    }    cout

Advertisements