C++ Articles

Page 287 of 597

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 1K+ 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

Read More

Check if a given string is sum-string in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 458 Views

Here we will see how to check whether a string is sum-string or not. A string is said to be sum-string if the rightmost substring can be written as the sum of two substrings before it, and the same is recursively true for substring before it. Suppose a string like 12243660 is a sum string, like 12 + 24 = 36, and the 36 is present after 12 and 24 in the string, again 24 + 36 = 60, this is also present in the string.A string S can be called sum-string, if it follows this rule −𝑠𝑢𝑏𝑠𝑡𝑟𝑖𝑛𝑔(𝑖, 𝑥)+𝑠𝑢𝑏𝑠𝑡𝑟𝑖𝑛𝑔(𝑥+1, 𝑗)= ...

Read More

Check if a given tree graph is linear or not in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 242 Views

Here we will see how to check whether a tree graph is linear or not. A linear tree graph can be expressed in one line, suppose this is an example of a linear tree graph.But this is not linear −To check a graph is linear or not, we can follow two conditionsIf the number of nodes is 1, then the tree graph is linearIf (n – 2) of its nodes have in-degree 2Example#include #include #define N 4 using namespace std; class Graph{    private:    int V;    vector *adj;    public:    Graph(int v){       ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 787 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#include #include using namespace std; void isTouchOrIntersect(int a, int ...

Read More

Check if a Matrix is Invertible in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 282 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#include #define ...

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
Arnab Chakraborty
Updated on 11-Mar-2026 229 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 Binary Tree (not BST) has duplicate value in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 382 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#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 Tree contains duplicate subtrees of size 2 or more in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 297 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#include #include using namespace std; const char MARKER = '$'; struct Node { ...

Read More

Check if a given mobile number is fancy in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 693 Views

We have a 10 digit mobile number, our task is to check whether the number is fancy number or not. There are three different conditions for a fancy number. If at least one is true, then the number is fancy. These conditions are like below −A single number occurs three consecutive times, like 555Three consecutive numbers are either in increasing or decreasing order like 123 or 321.A single digit occurs four or more times in a number, like 8965499259, here 9 has occurred four times.One example of fancy number is 9859009976, this is a fancy number as the third condition ...

Read More

Check if a given number can be represented in given a no. of digits in any base in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 211 Views

Suppose we have a number n, and number of digits d. We have to check whether the number n can be represented as d digit number in any base from 2 to 32. Suppose the number n is 8, and d = 4, then this can be represented as 1000 in binary, here the d is 4.The idea is to check all bases one by one from 2 to 32. We can follow these steps to check the base.If the number is smaller than base, and digit is 1, then return trueif digit is more than one and number is ...

Read More
Showing 2861–2870 of 5,962 articles
« Prev 1 285 286 287 288 289 597 Next »
Advertisements