Found 7347 Articles for C++

Find amount to be added to achieve target ratio in a given mixture in C++

Arnab Chakraborty
Updated on 24-Oct-2019 14:12:49

37 Views

Suppose we have a container with size X. It has a mixture of water and other liquid, the mixture has W% of water in it. We have to find how many water must be added to increase the ratio of water to Y%? If X = 125, W = 20 and Y = 25, then output will be 8.33 liters.Suppose we have to add A amount of water with the previous mixture, so new amount will be X + A. So the amount of water in the mixture will follow this formula.Old Amount+A=((W% of X) + A)Also the amount of ... Read More

Find all pairs (a,b) and (c,d) in array which satisfy ab = cd in C++

Arnab Chakraborty
Updated on 24-Oct-2019 13:23:15

118 Views

Suppose we have an array A, from that array, we have to choose two pairs (a, b) and (c, d), such that ab = cd. Let the array A = [3, 4, 7, 1, 2, 9, 8]. The output pairs are (4, 2) and (1, 8). To solve this, we will follow these steps −For i := 0 to n-1, dofor j := i + 1 to n-1, doget product = arr[i] * arr[j]if product is not present in the hash table, then Hash[product] := (i, j)if product is present in the hash table, then print previous and current elements.Example Live ... Read More

Find all pairs (a, b) in an array such that a % b = k in C++

Arnab Chakraborty
Updated on 24-Oct-2019 13:15:12

119 Views

Suppose we have an array A, from that array, we have to get all pairs (a, b) such that the a%b = k. Suppose the array is A = [2, 3, 4, 5, 7], and k = 3, then pairs are (7, 4), (3, 4), (3, 5), (3, 7).To solve this, we will traverse the list and check whether the given condition is satisfying or not.Example Live Demo#include using namespace std; bool displayPairs(int arr[], int n, int k) {    bool pairAvilable = true;    for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {          if (arr[i] % arr[j] == k) {             cout

Find all factorial numbers less than or equal to n in C++

Arnab Chakraborty
Updated on 24-Oct-2019 13:08:48

840 Views

Here we will see how to print all factorial numbers less than or equal to n, a number N is said to be factorial number if it is a factorial of a positive number. So some factorial numbers are 1, 2, 6, 24, 120.To print factorial numbers, we do not need to find the factorial directly. Starting from i = 1, print factorial*i. Initially factorial is 1. Let us see the code for better understanding.Example Live Demo#include using namespace std; void getFactorialNumbers(int n) {    int fact = 1;    int i = 2;    while(fact

Find All Duplicate Subtrees in C++

Arnab Chakraborty
Updated on 24-Oct-2019 13:05:38

142 Views

Consider we have a binary tree. We have to find if there are some duplicate subtrees in the tree or not. Suppose we have a binary tree like below −There are two identical subtrees of size 2. In each subtree D, BD and BE both are also duplicate subtrees We can solve this problem by using tree serialization and hashing process. We will store the inorder traversal of subtrees in the hash table. We will insert opening and closing parenthesis for empty nodes.Example Live Demo#include #include #include #include using namespace std; const char MARKER = '$'; struct ... Read More

Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0’s in C++

Arnab Chakraborty
Updated on 24-Oct-2019 13:02:41

95 Views

Here we will see how to generate one symmetric matrix of order N, and the elements of each row will contain numbers from 0 to N – 1. The diagonal elements will be 0 always.This task is easy, we will form a matrix of N x N, then for each row i and for each column j, if i and j are same, then mark it as 0, otherwise increase one counter from 1 to N – 1, place the values for each individual row.Example#include using namespace std; void makeSymmetricMatrix(int n) {    int matrix[n][n];    for(int i = 0; i

Find a subset with greatest geometric mean in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:58:09

105 Views

Here we have an array A with some elements. Our task is to find the subset where the geometric mean is maximum. Suppose A = [1, 5, 7, 2, 0], then the subset with greatest geometric mean will be [5, 7].To solve this, we will follow one trick, we will not find the mean, as we know that the largest two elements will form the greatest geometric mean, so the largest two elements will be returned as subset.Example#include using namespace std; void largestGeoMeanSubset(int arr[], int n) {    if (n < 2) {       cout max) ... Read More

Find a point such that sum of the Manhattan distances is minimize in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:55:39

474 Views

Suppose we have n different points in K dimension space, the value of n is in range (2, 105), and value of k in range (1 to 5). We have to determine the point such that the sum of Manhattan distance from resultant point to n points is minimized.The Manhattan distance between two points P1(x1, y1) and P2(x2, y2), is |x1 – x2| + |y1 – y2|. Suppose dimension is 3, and there are three points like (1, 1, 1), (2, 2, 2), (3, 3, 3), then the output will be (2, 2, 2).To solve this problem, we have to ... Read More

Find a pair with the given difference in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:53:24

288 Views

Consider we have an array A, there are n different elements. We have to find a pair (x, y) from the array A, such that the difference between x and y is same as given difference d. Suppose a list of elements are like A = [10, 15, 26, 30, 40, 70], and given difference is 30, then the pair will be (10, 40) and (30, 70)To solve this problem, we will assume that the array is sorted, then starting from left we will take two pointers to point elements, initially first one ‘i’ will point to the first element, ... Read More

Find a pair with maximum product in array of Integers in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:50:20

114 Views

Consider we have an array A, there are n different elements. We have to find a pair (x, y) from the array A, such that the product of x and y is maximum. The array may contain positive or negative elements. Suppose an array is like: A = [-1, -4, -3, 0, 2, -5], then the pair will be (-4, -5) as product is maximum.To solve this problem, we have to keep track four numbers, the positive_max, positive_second_max, negative_max, negative_second_max. At the end if the (positive_max * positive_second_max) is greater than (negative_max * negative_second_max), then return positive pairs, otherwise return ... Read More

Advertisements