C++ Articles

Page 274 of 597

Check if a Matrix is Invertible in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 266 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 217 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 365 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 284 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 672 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 196 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

Check if a given number is Pronic in C++

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

Here we will see, how to check whether a number is Pronic number or not. A number that can be arranged to form a rectangle, are called the pronic numbers. First few pronic numbers are: 0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342. The pronin numbers are product of two consecutive integers. So a pronic number n = x * (x + 1).Here we will check and generate some pronic numbers.Example#include #include using namespace std; bool isPronicNumber(int num) {    for (int i = 0; i

Read More

Check if a large number can be divided into two or more segments of equal sum in C++

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

Here we will see a program, that can check whether a number can be divided into more than one segments with equal sum. Suppose a number is like 74325, then this can be segmented into three parts (7), (4, 3), (2, 5), all are of same um value.We have to follow these steps to solve this problem.Take the number as stringuse an array to hold prefix sum of the arrayNow traversing from second element to last element, and the first segment will be 0 to i-1, whose sum will be placed at prefix_sum[i - 1]Use another variable which traverses from ...

Read More

Check if a large number is divisible by 13 or not in C++

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

Here we will see how to check a number is divisible by 13 or not. In this case the number is very large number. So we put the number as string.A number will be divisible by 13, if the number satisfies the following situations −A number is divisible by 13 if and only if we get the alternating sum i.e. alternatively adding and subtracting of blocks of three numbers from right to left is divisible by 13. For example, 2911285 is divisible by 13 because the alternating sum of blocks of size 3 is 2 – 911 + 285 = ...

Read More

Check if a linked list is Circular Linked List in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Here we will see, hoe to check a linked list is circular linked list or not. To check whether the linked list is circular or not, we will store the header node into some other variable, then traverse the list, if we get null at the next part of any node, then that is not circular, otherwise we will check the next node is same as the stored node or not, if so then that is circular.Example#include using namespace std; class Node{    public:    int data;    Node *next; }; Node* getNode(int data){    Node *newNode = new ...

Read More
Showing 2731–2740 of 5,961 articles
« Prev 1 272 273 274 275 276 597 Next »
Advertisements