Programming Articles - Page 2370 of 3366

Check if a given array can represent Preorder Traversal of Binary Search Tree in C++

Ravi Ranjan
Updated on 19-Aug-2025 17:22:30

569 Views

In this article, we have an array of preorder traversal of a binary search tree. Our task is to check if the given array can represent the preorder traversal of the binary search tree. In preorder traversal of tree, the root node is visited first, then the left subtree, and finally the right subtree. What is Binary Search Tree? A binary search tree is a tree data structure and a special type of binary tree that follows the conditions given below: The left child node's value is always less than the parent node. ... Read More

Check if a cell can be visited more than once in a String in C++

Arnab Chakraborty
Updated on 22-Oct-2019 10:58:43

194 Views

Suppose we have a string with dots (.) and a number, a dot indicates the cell is empty, and if there is a number x in any cell, it indicates that we can move x steps to the right or left within the string. Our task is to check whether we can visit a cell more than once or not. For example, if a string is like “. 2 . . . 2 . .”, then we can visit 4th cell in two different ways. From second cell to two step to right, or from two step left from cell ... Read More

Pretty print JSON using javax.json API in Java?

raja
Updated on 07-Jul-2020 07:58:47

977 Views

The javax.json package provides an Object Model API to process JSON. The Object Model API is a high-level API that provides immutable object models for JSON object and array structures. These JSON structures can be represented as object models using JsonObject and JsonArray interfaces. We can use the JsonGenerator interface to write the JSON data to an output in a streaming way. The JsonGenerator.PRETTY_PRINTING is a configuration property to generate JSON prettily.We can implement a pretty print JSON in the below example.Exampleimport java.io.*; import java.util.*; import javax.json.*; import javax.json.stream.*; public class JSONPrettyPrintTest {    public static void main(String args[]) {       String jsonString = ... Read More

Check if a binary tree is sorted levelwise or not in C++

Arnab Chakraborty
Updated on 22-Oct-2019 09:16:34

232 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 if a Binary Tree contains duplicate subtrees of size 2 or more in C++

Arnab Chakraborty
Updated on 22-Oct-2019 09:13:10

246 Views

Consider we have a binary tree. We have to find if there are some duplicate subtrees of size 2 or more in the tree or not. Suppose we have a binary tree like below −There are two identical subtrees of size 2. We can solve this problem by using tree serialization and hashing process. The idea is serializing the subtrees as string, and store them in hash table. Once we find a serialized tree which is not leaf, already exists in hash table, then return true.Example Live Demo#include #include using namespace std; const char MARKER = '$'; struct Node ... Read More

Check if a Binary Tree (not BST) has duplicate value in C++

Arnab Chakraborty
Updated on 22-Oct-2019 09:06:22

316 Views

Consider we have a binary tree, this binary tree is not a BST. We have to check whether the binary tree contains same element more than one time or not. To solve this, we will use hashing. We will traverse the given tree, for each node, we will check whether the node is present in the table or not, if that is already present, then return false, otherwise true.Example Live Demo#include #include using namespace std; class Node {    public:    int data;    Node *left;    Node *right; }; Node* getNode(int data){    Node *newNode = new Node; ... Read More

Check if a binary string contains all permutations of length k in C++

Arnab Chakraborty
Updated on 22-Oct-2019 09:03:53

153 Views

Suppose we have a binary string, another integer k. We have to check the string is containing all permutations of binary of k bits. Suppose a string is like “11001”, and if the K = 2, then it must have all permutations of k bit numbers. (00, 01, 10, 11), the given string has all permutation. So this is valid string.by taking the binary string and the value of k, we have to check whether binary sequences are matched or not. The binary sequence is made of size k. So there will be 2k number of different binary permutations. We ... Read More

Check if a number from every row can be selected such that xor of the numbers is greater than zero in C++

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

188 Views

Suppose we have one 2D array of size N x M. The task is to check if we can select a number from every row, in such a way that the XOR of those elements is non-zero or greater than 0. Suppose one matrix is like this −77710107If we perform XOR, the answer will be non-zero, as the except last elements of two rows are 7 and 10To solve this problem, the solution will be very easy. Initially check if XOR of the elements of the first column of each row is 0 or not. If it is non-zero then ... Read More

Check if a Matrix is Invertible in C++

Arnab Chakraborty
Updated on 07-Jul-2020 07:40:07

223 Views

Here we will see how to check whether a matrix is invertible or not. If one matrix is M, then the inverted matrix M-1 will be −$$M^-1=\frac{adj(M)}{|M\lvert}$$So if the determinant of M is non-zero, then only we can get the inverse, otherwise, we will not get the inverse of it. So here we have to check the determinant is non-zero or not. Finding a determinant is one recursive process. We have to find submatrix, then find the determinant of it, then use that result for the final calculation. Let us see the code to get a better idea.Example Live Demo#include ... Read More

Check if a line touches or intersects a circle in C++

Arnab Chakraborty
Updated on 22-Oct-2019 08:33:53

716 Views

Suppose we have a circle and another straight line. Our task is to find if the line touches the circle or intersects it, otherwise, it passes through outside. So there are three different cases like below −Here we will solve it by following steps. These are like below −Find perpendicular P between the center and given a lineCompare P with radius r −if P > r, then outsideif P = r, then touchesotherwise insideTo get the perpendicular distance, we have to use this formula (a center point is (h, k))$$\frac{ah+bk+c}{\sqrt{a^2+b^2}}$$Example Live Demo#include #include using namespace std; void isTouchOrIntersect(int a, ... Read More

Advertisements