Find Minimum Steps to Reach the End of a Matrix in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:35:01

417 Views

Suppose we have a 2D matrix with positive integers. We have to find the minimum steps required to move from to the end of the matrix (rightmost bottom cell), If we are at cell (i, j), we can go to the cell (i, j+mat[i, j]) or (i+mat[i, j], j), We cannot cross the bounds. So if the matrix is like −212111111The output will be 2. Path will be (0, 0) →(0, 2) → (2, 2)Here we will use the Dynamic programming approach to solve this. Suppose we are at cell (i, j), we want to reach (n-1, n-1) cell. We ... Read More

Find the Kth Node in Vertical Order Traversal of a Binary Tree in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:33:45

149 Views

Suppose we have a binary tree and a value K. The task is to print the Kth node in the vertical order traversal. If no such node exists, then return -1. So if the tree is like below −The vertical order traversal is like −4 2 1 5 6 3 8 7 9So if K = 3, then result will be 1.The approach is simple. We will perform the vertical order traversal, then check the current node is the kth node or not, if so then return.Example Live Demo#include #include #include #include using namespace std; class Node {    public:   ... Read More

Find Minimum Radius for At Least K Points Inside Circle in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:27:19

382 Views

Suppose we have some points, and one integer k. We have to find minimum radius of a circle whose center is at (0, 0) to cover k points. So if the points are like (1, 1), (-1, -1), (1, -1), and k = 3, then radius will be 2.Here we will find the Euclidean distance between each point and (0, 0), then sort the distances and return the kth element after sorting.Example Live Demo#include #include using namespace std; struct point{    int x, y; }; int minRadius(int k, point points[], int n) {    int dist[n];    for (int i = ... Read More

Find Minimum Number to be Divided for Perfect Square in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:24:24

2K+ Views

Suppose we have a number N. We have to find minimum number that divide N to make it perfect square. So if N = 50, then minimum number is 2, as 50 / 2 = 25, and 25 is a perfect square.A number is perfect square if it has even number of different factors. So we will try to find the prime factors of N, and find each prime factor power. Find and multiply all prime factors whose power is odd.Example Live Demo#include #include using namespace std; int findMinimumNumberToDivide(int n) {    int prime_factor_count = 0, min_divisor = 1;    while ... Read More

Find First, Second, and Third Minimum Elements in an Array in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:21:51

262 Views

Suppose we have an array of n elements. We have to find the first, second and the third minimum elements in the array. First minimum is the minimum of the array, second min is minimum but larger than the first one, and similarly the third min is minimum but greater than second min.Scan through each element, then check the element, and relate the condition for first, second and third min elements conditions to solve this problem.Example#include using namespace std; int getThreeMins(int arr[], int n) {    int first = INT_MAX, sec = INT_MAX, third = INT_MAX;       for ... Read More

First Circular Tour Visiting All Petrol Pumps in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:20:58

452 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 another.Calculate 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 Minimum Number of Log Values Needed to Calculate Log Up to N in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:19:45

142 Views

As we know that log(x*y) = log(x) + log(y). So we will see what are the minimum number of log values are required to calculate all log values from 1 to N. So if N is 6, then output will be 3, as from log(1) to log(6), there are three log values are required except log(1). As log(1) is always 0, then ignore it, now for log(2) and log(3), we have to find. After that for log(4) this is log(2)+ log(2), but value of log(2) is known, so we do not calculate this again, for log(5), we need to calculate. ... Read More

Final Radiations of Each Radiated Station in C++ Program

Arnab Chakraborty
Updated on 18-Dec-2019 11:18:32

171 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 Minimum Moves to Reach Target on an Infinite Line in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:17:00

426 Views

Suppose we have a number position in infinite number line. (-inf to +inf). Starting from 0, we have to reach to the target by moving as described. In ith move, we can go i steps either left or right. We have to find minimum number of moves that are required. Suppose the target is 2, so minimum steps will be 3. From 0 to 1, from 1 to -1 and from -1 to 2.To solve this problem, we have some important points to remember. If the target is negative, then just take this as positive, as the number line is ... Read More

Find Maximum Volume of a Cuboid from Given Perimeter and Area in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:14:07

467 Views

Suppose we have the area A and a perimeter P, now we have to find what will be the maximum volume that can be made in form of cuboid from given perimeter and surface area. So when the P is 24 and A is 24, then the output will be 8.As we know for given perimeter of cuboid P = 4(length + breadth + Depth), for area, it will be A = 2(length* breadth + breadth*Depth + length *Depth), and the volume is V = (length* breadth*Depth)Example Live Demo#include #include using namespace std; float maxVolumeCuboid(float Peri, float Area) {    float ... Read More

Advertisements