Programming Articles - Page 2222 of 3363

Find the missing number in Geometric Progression in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:48:57

177 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 pair with maximum difference in any column of a Matrix in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:50:47

145 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 Arithmetic Progression in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:47:11

243 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 out the minimum number of coins required to pay total amount in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:46:38

324 Views

Suppose we have a number N, and unlimited number of coins worth 1, 10 and 25 currency coins. Find minimum number of coins we need to use to pay exactly amount N. Suppose N is 14, then number of coins will be 5, as one 10 value coin and four 1 value coin.To solve this, we have to use these steps −If N < 10, then return N number of 1 value coinsIf N > 9 and N < 25, then divide the value with 10, and get the result, the remaining will be covered using 1 value coin, add ... Read More

Find the minimum number of steps to reach M from N in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:45:29

887 Views

Suppose we have two integers N and M. We have to find minimum number of steps to reach M from N, by performing given operations −Multiply the number x by 2, so x will be 2*xSubtract one from the number x, so the number will be x – 1If N = 4 and M = 6, then output will be 2. So if we perform operation number 2 on N, then N becomes 3, then perform operation number one on updated value of N, so it becomes 2 * 3 = 6. So the minimum number of steps will be ... Read More

Find number of diagonals in n sided convex polygon in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:44:23

424 Views

Suppose we have a number n, and we have to find the number of diagonals for n sided convex polygon. So if the n = 5, then diagonals count will be 5.As this is n-sided convex polygon, from each vertex we can draw n – 3 diagonals leaving two sided adjacent vertices and itself. So for n vertices, it will be n*(n-3), but as we are considering twice, so it will be n(n – 3)/2.Example Live Demo#include using namespace std; int diagonalCount(int n) {    return n * (n - 3) / 2; } int main() {    int n = 8;    cout

Find the minimum distance between two numbers in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:43:26

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 the longest sub-array having exactly k odd numbers in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:41:44

141 Views

Suppose we have one array with n elements. The problem is to find longest sub-array which has exactly k odd numbers. So if A = [2, 3, 4, 11, 4, 12, 7], and k = 1, then output will be 4, sub-array is [4, 11, 4, 12]We can solve this using sliding window. The task is like below −Initialize max := 0, count := 0, and start := 0for i in range 0 to n – 1, do the followingif arr[i] mod 2 is not 0, then increase count by 1while count > k and start k && start

Find the longest path in a matrix with given constraints in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:38:25

556 Views

Suppose we have one square matrix of order n. It has all distinct elements. So we have to find the maximum length path, such that all cells along the path are in increasing order with a difference of 1. From one cell we can move to four directions. Left, Right, Top and Bottom. So if the matrix is like −129538467So the output will be 4. As the longest path is 6→7→8→ 9To solve this problem, we will follow this idea. We will calculate longest path beginning with every cell. Once we have got the longest for all cells, we return ... Read More

Find number of cavities in a matrix in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:39:31

156 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

Advertisements