Server Side Programming Articles - Page 2142 of 2651

Check if a given directed graph is strongly connected in C++

Arnab Chakraborty
Updated on 22-Oct-2019 08:16:56

405 Views

Suppose we have a graph. We have to check whether the graph is strongly connected or not. A graph is said to be strongly connected, if any two vertices have a path between them, then the graph is connected. An undirected graph is strongly connected graph. Some undirected graph may be connected but not strongly connected. This is an example of a strongly connected graph.This is an example of a connected, but not strongly connected graph.Here we will see, how to check a graph is strongly connected or not using the following steps.Steps −Mark all nodes as not visitedStart DFS ... Read More

Check if a given circle lies completely inside the ring formed by two concentric circles in C++

Arnab Chakraborty
Updated on 22-Oct-2019 08:12:55

986 Views

We have two circles. The center of both of them is at the origin. The radius of these two circles is given. They are r and R, R > r. Another circle is also present. Its radius (r1) and the center point are given, we have to check whether that point is inside the ring formed by the first two circles or not.We can solve this using the Pythagorean theorem. compute the distance from the center of the circle and origin. Then if (distance – r1) >= r and (distance – r1) = R && dis+r1

Check if a given Binary Tree is SumTree in C++

Arnab Chakraborty
Updated on 22-Oct-2019 08:05:45

228 Views

Here we will see how to check whether a binary tree is sum-tree or not. Now the question is what is the sum-tree. A sum-tree is a binary tree where a node will hold the sum value of its children. The root of the tree will contain an entire sum of all elements below it. This is an example of sum-tree −To check this, we will follow a simple trick, we will find the sum of left and right subtree elements if the sum value is the same as the root, then that is sum-tree. This will be one recursive ... Read More

Check if a given array contains duplicate elements within k distance from each in C++

Arnab Chakraborty
Updated on 22-Oct-2019 08:02:29

445 Views

Here we will see how to check whether an unsorted array has duplicate elements within k distance from each other or not. Suppose a list of elements is {1, 2, 3, 1, 4, 5}, here if k = 3, then the program will return true, as the distance between two 1s is 3.We will solve this using a hash table. The steps will be like below −Create one empty hash tableFor each index i, let the element e = arr[i] in the list, doif e is present in the hash table, then return trueotherwise, add e to hash table, and ... Read More

Check if a doubly linked list of characters is palindrome or not in C++

Arnab Chakraborty
Updated on 22-Oct-2019 07:54:29

727 Views

Here we will see how to check a string is a palindrome or not using a Doubly linked list. Here we will push each character of a string inside one doubly linked list. There will be two pointers, left and right. Then start scanning from both sides. If a left character is the same as right character, then move the left pointer to the next node, and move the right pointer to the previous node. Otherwise, return false. This process will be continued until the left and right are pointing at the same node, or the right pointer is pointing ... Read More

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

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

782 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

154 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

Check horizontal and vertical symmetry in binary matrix in C++

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

280 Views

Suppose we have a binary matrix of order M x N. The task is to check whether the matrix is horizontally symmetric, vertically symmetric or both. One matrix is said to be horizontally symmetric if the ith row is the same as the (M – i)th row, this is said to be vertically symmetric if the jth column is the same as the (N – j)th column. Suppose the input matrices are like below −011101011This is Horizontally symmetric.111101111This is Vertically symmetric.111101111This is Horizontally as well as vertically symmetric.We will solve this problem using two phases. At first, we will check ... Read More

Flip-flop types and their Conversion in C++

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

2K+ 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

248 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

Advertisements