Found 7347 Articles for C++

Check if a binary tree is subtree of another binary tree in C++

Arnab Chakraborty
Updated on 22-Oct-2019 07:52:12

679 Views

Suppose we have two binary trees. We have to check whether the smaller tree is a subtree of another binary tree or not. Consider these two trees are given.There are two trees. The second tree is the subtree of the first one. To check this property, we will traverse the tree in post-order fashion, then if the subtree rooted with this node is identical to the second tree, then it is subtree.Example Live Demo#include using namespace std; class node {    public:    int data;    node *left, *right; }; bool areTwoTreeSame(node * t1, node *t2) {    if (t1 ... Read More

Check if a binary tree is sorted level-wise or not in C++

Arnab Chakraborty
Updated on 22-Oct-2019 07:49:00

82 Views

Here we will see how to check a binary tree is level wise sorted or not. The level wise sorted binary tree will be look like below −In each level, the nodes are sorted from left to right, and each layer contains higher value than its previous level.We can solve this problem by performing level order traversal, and keep track of the minimum and maximum elements of current level. Use another variable prev_max to hold maximum value of the previous level. Then compare the minimum value of current level and maximum value of previous level prev_max. If min value is ... Read More

Flip-flop types and their Conversion in C++

Arnab Chakraborty
Updated on 22-Oct-2019 07:41:03

1K+ Views

Flip-Flops are sequential digital circuits. There are few different types of flip-flops. Here we will see the types of flip-flops and the conversion rules from one flip-flop to another.There are basically four types of flip-flops −SR Flip-FlopD Flip-FlopJK Flip-FlopT Flip-FlopSR Flip-flopSR flip-flop operates with only positive clock transitions or negative clock transitions. Whereas, SR latch operates with enable signal. The circuit diagram of SR flip-flop is shown in the following figure.This circuit has two inputs S & R and two outputs Q(t) & Q(t)’. The operation of SR flipflop is similar to SR Latch. But, this flip-flop affects the outputs ... Read More

Finding Quadrant of a Coordinate with respect to a Circle in C++

Arnab Chakraborty
Updated on 22-Oct-2019 07:26:16

163 Views

We have one circle (center coordinate and radius), we have to find the quadrant of another given point (x, y) lies with respect to the center of the circle, if this is present in the circle, print quadrant, otherwise print error as the point is present outside.Suppose the center of the circle is (h, k), the coordinate of the point is (x, y). We know that the equation of the circle is −(𝑥−ℎ)2+(𝑦−𝑘)2+𝑟2=0Now there are few conditions, based on which we can decide the result.𝑖𝑓 (𝑥−ℎ)2+(𝑦−𝑘)2> 𝑟, 𝑡ℎ𝑒𝑛 𝑡ℎ𝑒 𝑝𝑜𝑖𝑛𝑡 𝑖𝑠 𝑜𝑢𝑡𝑠𝑖𝑑𝑒 𝑡ℎ𝑒 𝑐𝑖𝑟𝑐𝑙𝑒𝑖𝑓 (𝑥−ℎ)2+(𝑦−𝑘)2= 0, 𝑡ℎ𝑒𝑛 𝑡ℎ𝑒 𝑝𝑜𝑖𝑛𝑡 𝑖𝑠 ... Read More

Find Simple Closed Path for a given set of points in C++

Arnab Chakraborty
Updated on 22-Oct-2019 07:21:48

253 Views

Consider we have a set of points. We have to find a simple closed path covering all points. Suppose the points are like below, and the next image is making a closed path on those points.To get the path, we have to follow these steps −Find the bottom left point as PSort other n – 1 point based on the polar angle counterclockwise around P, if polar angle of two points are same, then put them as the distance is shortestTraverse the sorted list of points, then make the pathExample Live Demo#include using namespace std; class Point {    public: ... Read More

Find minimum and maximum elements in singly Circular Linked List in C++

Arnab Chakraborty
Updated on 22-Oct-2019 07:14:25

336 Views

Here we will see how to get the minimum and maximum value from one singly circular linked linear list. The basic concept is very simple. The next part of the last node will be pointed to the first node, the first node will also be pointed using start pointer. When we insert some element into the list, after inserting the next part of the newly inserted node will be updated with the address of the start node.Initially the min is assigned with positive infinity, and max is assigned with negative infinity. Now traverse the list from left to right. If ... Read More

Find K Closest Points to the Origin in C++

Arnab Chakraborty
Updated on 22-Oct-2019 07:05:18

281 Views

Suppose we have a set of points. Our task is to find K points which are closest to the origin. Suppose the points are (3, 3), (5, -1) and (-2, 4). Then the closest two (K = 2) points are (3, 3), (-2, 4).To solve this problem, we will sort the list of points based on their Euclidean distance, after that take the top most K elements from the sorted list. Those are the K nearest points.Example Live Demo#include #include using namespace std; class Point{    private:    int x, y;    public:    Point(int x = 0, int y = 0){       this->x = x;       this->y = y;    }    void display(){       cout

Minimize Cash Flow among a given set of friends who have borrowed money from each other in C++

Arnab Chakraborty
Updated on 22-Oct-2019 06:59:30

911 Views

Suppose there are few friends. They have borrowed money from each other. So there will be some cash flow on the network. Our task is to minimize the cash flow in the network. Suppose there are three friends P1, P2, and P3. The cash flow among them is like below −This cash flow is not minimum; we have to minimize it. Then the final diagram will be like −To solve this problem we will use the greedy approach. Here in each step settle all amounts of one person and recur for remaining n-1 persons. Now the question comes, how to ... Read More

Check if a point is inside, outside or on the parabola in C++

Arnab Chakraborty
Updated on 22-Oct-2019 06:45:39

281 Views

Suppose, one parabola is given (the vertex coordinate (h, k) and distance from focus and vertex is a), another point is also given. We have to find whether the point is inside the parabola or not. To solve it, we have to solve the following equation for the given point (x, y)\left(y-k\right)^2=4a\left(x-h\right)If the result is less than 0, then this is present inside the parabola if it is 0, then it is on the parabola, and if greater than 0, then outside of the parabola.Example Live Demo#include #include using namespace std; int isInsideParabola(int h, int k, int x, int ... Read More

Check if a point is inside, outside or on the ellipse in C++

Arnab Chakraborty
Updated on 22-Oct-2019 06:42:15

656 Views

Suppose, one ellipse is given (the center coordinate (h, k) and semi-major axis a, and semi-minor axis b), another point is also given. We have to find whether the point is inside the ellipse or not. To solve it, we have to solve the following equation for the given point (x, y).$$\frac{\left(x-h\right)^2}{a^2}+\frac{\left(y-k\right)^2}{b^2}\leq1$$If the result is less than one, then the point is inside the ellipse, otherwise not.Example Live Demo#include #include using namespace std; bool isInsideEllipse(int h, int k, int x, int y, int a, int b) {    int res = (pow((x - h), 2) / pow(a, 2)) + ... Read More

Advertisements