Found 7347 Articles for C++

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

135 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

92 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

467 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++

Arnab Chakraborty
Updated on 22-Oct-2019 11:05:49

127 Views

Suppose we have a square matrix, our task is to check whether the matrix is Hankel matrix or not. The Hankel matrix is a square matrix, in which each ascending skew-diagonal elements from left to right is constant. Suppose a matrix is like below −1234523456345674567856789To check whether the matrix is Hankel Matrix or not, we have to check whether mat[i, j] = ai+j or not. ai+j can be defined as −$$a_{i+j}=\begin{cases}mat[i+j, 0]< n\mat[i+j-n+1, n-1]otherwise\end{cases}$$Example Live Demo#include #define N 5 using namespace std; bool isHankelMat(int mat[N][N], int n) {    for (int i = 0; i < n; i++) {   ... Read More

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

Arnab Chakraborty
Updated on 22-Oct-2019 11:01:12

439 Views

Suppose we have a list of elements in an array, we have to check, whether the elements can be the preorder traversal of a binary search tree or not. Suppose a sequence is like {40, 30, 35, 80, 100}, then the tree will be like −We can solve this using a stack. We have to follow these steps to solve this problem.Define empty stackset root as negative infinityfor every element in preorder sequence, do the following −if the element is smaller than current root, return falseKeep removing elements from stack while the element is greater than stack top, and make ... 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

116 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

146 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

124 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

203 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

92 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

Advertisements