Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by Arnab Chakraborty
Page 346 of 377
Check if a given Binary Tree is SumTree in C++
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 MoreCheck if a given array contains duplicate elements within k distance from each in C++
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 MoreCheck if a doubly linked list of characters is palindrome or not in C++
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 MoreCheck if a binary tree is subtree of another binary tree in C++
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 MoreCheck if a binary tree is sorted level-wise or not in C++
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 MoreCheck horizontal and vertical symmetry in binary matrix in C++
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 MoreFlip-flop types and their Conversion in C++
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 MoreFinding Quadrant of a Coordinate with respect to a Circle in C++
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 MoreFind Simple Closed Path for a given set of points in C++
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 MoreFind minimum and maximum elements in singly Circular Linked List in C++
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