C++ Articles

Page 549 of 597

Find smallest subarray that contains all elements in same order in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Dec-2019 425 Views

Suppose we have two arrays of size m and n, The task is to find minimum length subarray in the first array, that contains all the elements if the second array. Element in second array may be present in the large array in non-contiguous but order must be same. So if two arrays are like A = [2, 2, 4, 5, 8, 9], and B = [2, 5, 9], then the output will be 5. As the smallest subarray of A, will be [2, 4, 5, 8, 9]. Here all elements like [2, 5, 9] are in the same order. ...

Read More

Find the Nth term of the series where each term f[i] = f[i – 1] – f[i – 2] in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Dec-2019 175 Views

Suppose we have a series called f. Each term of f, follows this rule f[i] = f[i – 1] – f[i – 2], we have to find the Nth term of this sequence. f[0] = X and f[1] = Y. If X = 2 and Y = 3, and N = 3. The result will be -2.If we see this closely, there will be almost six terms before the sequence starts repeating itself. So we will find the first 6 terms of the series and then the Nth term will be the same as (N mod 6)th term.Example#include< iostream> using ...

Read More

Find the altitude and area of an isosceles triangle in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Dec-2019 402 Views

Consider we have the side of the isosceles triangle, our task is to find the area of it and the altitude. In this type of triangle, two sides are equal. Suppose the sides of the triangle are 2, 2 and 3, then altitude is 1.32 and the area is 1.98. Altitude(h)=$$\sqrt{a^{2}-\frac{b^{2}}{2}}$$ Area(A)=$\frac{1}{2}*b*h$ Example #include #include using namespace std; float getAltitude(float a, float b) {     return sqrt(pow(a, 2) - (pow(b, 2) / 4)); } float getArea(float b, float h) {     return (1 * b * h) / 2; } int main() {     float a = 2, b = 3;     cout

Read More

Find minimum difference between any two element in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Dec-2019 824 Views

Suppose we have an array of n elements called A. We have to find the minimum difference between any two elements in that array. Suppose the A = [30, 5, 20, 9], then the result will be 4. this is the minimum distance of elements 5 and 9.To solve this problem, we have to follow these steps −Sort the array in non-decreasing orderInitialize the difference as infiniteCompare all adjacent pairs in the sorted array and keep track of the minimum oneExample#include #include using namespace std; int getMinimumDifference(int a[], int n) {    sort(a, a+n);    int min_diff = INT_MAX;    for (int i=0; i

Read More

Find three closest elements from given three sorted arrays in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Dec-2019 456 Views

Suppose we have three sorted arrays A, B and C, and three elements i, j and k from A, B and C respectively such that max(|A[i] – B[i]|, |B[j] – C[k]|, |C[k] – A[i]|) is minimized. So if A = [1, 4, 10], B = [2, 15, 20], and C = [10, 12], then output elements are 10, 15, 10, these three from A, B and C.Suppose the size of A, B and C are p, q and r respectively. Now follow these steps to solve this −i := 0, j := 0 and k := 0Now do the following ...

Read More

Find pair with maximum difference in any column of a Matrix in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 164 Views

Suppose we have one matrix or order NxN. We have to find a pair of elements which forms maximum difference from any column of the matrix. So if the matrix is like −123535967So output will be 8. As the pair is (1, 9) from column 0.The idea is simple, we have to simply find the difference between max and min elements of each column. Then return max difference.Example#include #define N 5 using namespace std; int maxVal(int x, int y){    return (x > y) ? x : y; } int minVal(int x, int y){    return (x > y) ? ...

Read More

Find the missing number in Geometric Progression in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 206 Views

Suppose we have an array that represents elements of geometric progression in order. One element is missing. We have to find the missing element. So if arr = [1, 3, 27, 81], output is 9, as 9 is missing.Using binary search, we can solve this problem. We will go to the middle element, then check whether the ratio between middle and next to the middle is same as common ratio or not. If not, then missing element is present between indices mid and mid + 1. If the middle element is the n/2th element in the GP, then missing element ...

Read More

Find the missing number in Arithmetic Progression in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 280 Views

Suppose we have an array that represents elements of arithmetic progression in order. One element is missing. We have to find the missing element. So if arr = [2, 4, 8, 10, 12, 14], output is 6, as 6 is missing.Using binary search, we can solve this problem. We will go to the middle element, then check whether the difference between middle and next to the middle is same as diff or not. If not, then missing element is present between indices mid and mid + 1. If the middle element is the n/2th element in the AP, then missing ...

Read More

Find the minimum distance between two numbers in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 1K+ Views

Suppose we have one unsorted array A, and two numbers x and y. We have to find the minimum distance between x and y in A. The array can also contain duplicate elements. So if the array is A = [2, 5, 3, 5, 4, 4, 2, 3], x = 3 and y = 2, then the minimum distance between 3 and 2 is just 1.To solve this, we have to follow these steps, Traverse the array from left to right and stop if either x or y has found. Then store the index of that position into prevNow traverse ...

Read More

Find number of cavities in a matrix in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 188 Views

Consider one matrix is given. We have to find the number of cavities in the matrix. One element is denoted as cavity when all other elements surrounding it is greater than the element. So if the matrix is like −456715456So the output is 1.We simply check the surrounding elements and form the decision.Example#include #define MAX 100 using namespace std; int numberOfCavities(int array[][MAX], int n) {    int arr[n + 2][n + 2];    int count = 0;    for (int i = 0; i < n + 2; i++) {       for (int j = 0; j < ...

Read More
Showing 5481–5490 of 5,962 articles
« Prev 1 547 548 549 550 551 597 Next »
Advertisements