Found 7197 Articles for C++

Find the node whose absolute difference with X gives maximum value in C++

Arnab Chakraborty
Updated on 17-Dec-2019 12:13:47

99 Views

Suppose we have a tree, and the weights of all the nodes and an integer x. We have to find the node i, such that |weight[i] - x| is minimum. If the graph is like below, and x = 15Output will be 3. Now for different nodes, it will be like belowNode 1, |5 – 15| = 10Node 2, |10 – 15| = 5Node 3, |11 – 15| = 4Node 4, |8 – 15| = 7Node 5, |6 – 15| = 9The idea is simple. We will perform the DFS on the tree, and keep track of the node, whose ... Read More

Find the minimum value to be added so that array becomes balanced in C++

Arnab Chakraborty
Updated on 17-Dec-2019 12:08:13

212 Views

Suppose we have an array A with n elements. And the n is even. We have to find the value that needs to balance the array. As the size of the array is even, then we can make two halves. Sum of the left half and sum of the right half needs to be balanced. So if the array is like A = [1, 2, 3, 2, 5, 3] The sum of left half is 6, and sum of right half is 10. so we need 4 to balance the array.The task is simple, we will find the sum of ... Read More

Find the mean vector of a Matrix in C++

Arnab Chakraborty
Updated on 17-Dec-2019 12:05:50

180 Views

Suppose we have a matrix of order M x N, we have to find the mean vector of the given matrix. So if the matrix is like −123456789Then the mean vector is [4, 5, 6] As the mean of each column is (1 + 4 + 7)/3 = 4, (2 + 5 + 8)/3 = 5, and (3 + 6 + 9)/3 = 6From the example, we can easily identify that if we calculate the mean of each column will be the mean vector.Example Live Demo#include #define M 3 #define N 3 using namespace std; void calculateMeanVector(int mat[M][N]) {    cout

Find the maximum element in an array which is first increasing and then decreasing in C++

Arnab Chakraborty
Updated on 17-Dec-2019 12:02:44

346 Views

Suppose we have one array, which is initially increasing then decreasing. We have to find the max value in the array. So if the array elements are like A = [8, 10, 20, 80, 100, 250, 450, 100, 3, 2, 1], then output will be 500.We can use the binary search to solve this. There are three conditions −When mid is greater than both of its adjacent elements, then mid is maximumIf mid is greater than the next element, but smaller than previous element, then max lies on the left side of mid.If mid element is smaller than the next ... Read More

Find the first circular tour that visits all petrol pumps in C++

Arnab Chakraborty
Updated on 17-Dec-2019 12:00:34

210 Views

Suppose there is a circle, and there are n petrol pumps on the circle. We have two sets of data like −The amount of petrol that every petrol pump hasDistance from one petrol pump to anotherCalculate the first point, from where a truck will be able to complete the circle. Assume for 1 liter petrol, the truck can go 1 unit of distance. Suppose there are four petrol pumps, and the amount of petrol, and distance from the next petrol pump is as like [(4, 6), (6, 5), (7, 3), (4, 5)], the first point from where truck can make ... Read More

Find the final radiations of each Radiated Stations in C++

Arnab Chakraborty
Updated on 17-Dec-2019 11:57:08

135 Views

Suppose there are N stations in the straight line. Each of them has same non-negative power of radiation power. Every station can increase the radiation power of its neighboring stations in the following way.Suppose the station i with radiation power R, will increase (i – 1)th station’s radiation power, by R-1, (i - 2)th station’s radiation power by R-2, and will increase (i + 1)th station’s radiation power, by R-1, (i + 2)th station’s radiation power by R-2. So on. So for example, if the array is like Arr = [1, 2, 3], then the output will be 3, 4, ... Read More

Find the count of maximum contiguous Even numbers in C++

Arnab Chakraborty
Updated on 17-Dec-2019 11:37:01

337 Views

Suppose we have an array A with n elements. We have to find the maximum number of the contiguous even numbers in the given array. So if the array is like A = [1, 2, 3, 4, 6, 8, 7], then the count will be 3.We can solve this easily. We need two count variables one is max_current, and another is max_till_now. If an even number is found, then increase max_current, then compare it with max_till_now. Every time an odd element is found reset max_count to 0.Example Live Demo#include using namespace std; int maxEvenContiguous(int arr[], int n) {    int max_current ... Read More

Find the area of largest circle inscribed in ellipse in C++

Arnab Chakraborty
Updated on 17-Dec-2019 11:23:47

162 Views

Suppose we have an ellipse, with major and minor axis length 2a & 2b. We have to find the area of the largest circle that can be inscribed in it. So if the a = 5 and b = 3, then area will be 28.2734From the we can see that the radius of the maximum area circle inscribed in an ellipse will be the minor axis ‘b’. So the area will be A = π*b*bExample Live Demo#include using namespace std; double inscribedCircleArea(double b) {    double area = 3.1415 * b * b;    return area; } int main() {    double a = 10, b = 8;    cout

Find sum of all nodes of the given perfect binary tree in C++

Arnab Chakraborty
Updated on 17-Dec-2019 11:21:48

204 Views

Suppose we have a positive integer L, which represents the number of levels in a perfect binary tree. The leaf nodes in this perfect binary tree are numbered starting from 1 to n. Where the n is number of leaf nodes. The parent node is the sum of children. Our task is to write a program to print the sum of all of the nodes of this perfect binary tree. So if the tree is like below −So total sum is 30.If we see closer, we need to find the sum of all of the nodes. As the leaf nodes ... Read More

Find relative complement of two sorted arrays in C++

Arnab Chakraborty
Updated on 17-Dec-2019 11:16:34

444 Views

Suppose we have two sorted arrays arr1 and arr2, there sizes are m and n respectively. We have to find relative complement of two arrays. It means that we need to find all those elements which are present in arr1, but not in arr2. So if the arrays are like A = [3, 6, 10, 12, 15], and B = [1, 3, 5, 10, 16], then result will be [6, 12, 15]To solve this, we can use the set_difference function. As the problem is basically set difference operation.Example Live Demo#include #include #include using namespace std; int main() {    int first[] ... Read More

Advertisements