Found 7197 Articles for C++

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

Arnab Chakraborty
Updated on 22-Oct-2019 11:20:20

155 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 given number is Pronic in C++

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

879 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 Live Demo#include #include using namespace std; bool isPronicNumber(int num) {    for (int i = 0; i

Check if a given number divides the sum of the factorials of its digits in C++

Arnab Chakraborty
Updated on 22-Oct-2019 11:14:52

165 Views

Suppose, we have an integer, we have to find if the number divides the sum of the factorial of its digits. Suppose a number is 19, the sum of factorial of digits is (1! + 9!) = 362881, this is divisible by 19.To solve this, we will take the number, then calculate factorial of each digit and add the sum, if the sum is divisible by the number itself, then return true, otherwise false.Example#include using namespace std; int factorial(int n){    if(n == 1 || n == 0)       return 1;    return factorial(n - 1) * ... Read More

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

Arnab Chakraborty
Updated on 22-Oct-2019 11:11:40

160 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 mobile number is fancy in C++

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

616 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 matrix is Hankel or not in C++

Akansha Kumari
Updated on 23-Jul-2025 18:44:13

227 Views

The Hankel matrix is a square matrix, in which each ascending skew-diagonal or anti-diagonal (from top-right to bottom-left) elements are constant. For example a matrix name M with size 5x5 is given below − 1 2 3 4 ... Read More

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

559 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

183 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

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

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

220 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

229 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

Advertisements