C++ Articles

Page 566 of 597

Check given array of size n can represent BST of n levels or not in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Oct-2019 147 Views

We have an array A, we have to check whether the array can represent a BST with n levels or not. As the level is , we can construct a tree in following manner. Assume a number is k, value greater than k moves to right side, and less than k moves to left side. Suppose two lists are there: {50, 20, 9, 25, 10}, and {50, 30, 20, 25, 10}The first one is not valid, but the second one is valid.To check this we can either create a BST and check the height, otherwise use array based approach. The ...

Read More

Check for Identical BSTs without building the trees in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Oct-2019 227 Views

We have two arrays to represent the elements of the BST. If we take elements from that array from left to right, and form the BST, then by taking from both the arrays, we will make the same BST. We have to check whether both are forming the same or not. But the constraint is we cannot make the BST. Suppose two arrays are {2, 4, 1, 3}, and {2, 1, 4, 3}, then if we see, both of these sequences can form same BST.The approach is simple. As we know, the elements of left subtree are smaller than root ...

Read More

Check for Children Sum Property in a Binary Tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Oct-2019 210 Views

Suppose we have a binary tree. The binary tree is valid when it meets the following property.Each node should contain data value same as the sum of left and right children values. If there are no children at any side, then it will be treated as 0.Suppose a tree is present like below, which meets the given property.There is no such trick to check this, we have to traverse the tree recursively, if the node and both of its children satisfies the property then return true, otherwise false.Example#include using namespace std; class node {    public:    int data; ...

Read More

Finding a Non Transitive Coprime Triplet in a Range in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Oct-2019 188 Views

Suppose we have the lower and upper bound, and we have to find nontransitive triplet (x, y, z), such that the pair (x, y) are coprime (GCD is 1), the pair (y, z) are coprime, but pair (x, z) is not a coprime pair. For example, if the lower bound is 2, and upper bound is 10, then the elements are {2, 3, 4, 5, 6, 7, 8, 9, 10}, here possible triplet is (4, 7, 8), here (4, 7), and (7, 8) are coprime, but (4, 8) is not a coprime pair.We will follow the naïve approach to solve ...

Read More

Minimize ASCII values sum after removing all occurrences of one character in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Oct-2019 207 Views

Suppose we have a string. We have to minimize the sum of ASCII values, of each character to the string, after removing every occurrence of a particular character. Suppose a string is given like “hello” the sum of ASCII characters is (104 + 101 + 108 + 108 + 111) = 532. Now check occurrences of each characters.h has occurred one time, so cost is 1 * 104 = 104e has occurred one time, so cost is 1 * 101 = 101l has occurred one time, so cost is 2 * 108 = 216o has occurred one time, so cost ...

Read More

Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Oct-2019 292 Views

Suppose we have a complete graph; we have to count number of Edge Disjoint Spanning trees. The Edge Disjoint Spanning trees are spanning trees, where no two trees in the set have an edge in common. Suppose the N (number of vertices) is 4, then output will be 2. The complete graph using 4 vertices is like below −Two edge disjoint spanning trees are like −The maximum number of edge disjoint spanning tree from a complete graph, with N vertices will be $[\frac{n}{2}]$Example#include #include using namespace std; int maxEdgeDisjointSpanningTree(int n){    return floor(n/2); } int main() {    int n = 4;    cout

Read More

Check if a number is formed by Concatenation of 1, 14 or 144 only in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Oct-2019 275 Views

Here we will see one problem, that can tell that whether a string or a number is a concatenation of 1, 14 or 144 only. Suppose a string is “111411441”, this is valid, but “144414” is not valid.The task is simple, we have to fetch a single digit, double-digit and triple-digit number from the last, and check whether they match with any of these three (1, 14 and 144), if we get one match, divide the number with it, and repeat this process until the entire number is not exhausted.Example#include #include using namespace std; bool checkNumber(long long number) ...

Read More

Check if a number can be expressed as sum two abundant numbers in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Oct-2019 223 Views

Suppose we have a number. We have to express this as sum of two Abundant number, if yes, print the numbers, otherwise print -1. A number is said to be Abundant number is sum of all proper divisor of the number, denoted by sum(n) is greater than the value of number.To solve this, we will store all abundant numbers into a set, and for given number n, run a loop for i = 1 to n, and check n and (n – i) are abundant or not.Example#include #include #define N 100005 using namespace std; set getAbundantSet() {   ...

Read More

Check if a given string is a rotation of a palindrome in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Oct-2019 405 Views

Here we will see, one string is palindrome after certain rotation or not. A palindrome is a string that is the same in both directions. A string rotation is a palindrome if that is like AAAAD. this is not a palindrome directly, but its rotation AADAA is a palindrome.To check a string is rotated palindrome or not, then we will check this is a palindrome or not at the first time, after that, rotate it by one character, then check again, this checking will be performed n amount of time, where n is the number of characters.Example#include #include ...

Read More

Check if a circle lies inside another circle or not in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Oct-2019 680 Views

Suppose we have two circles (center points, and the radius values), we have to check one circle is fit inside another circle or not. There are three possible causes.The smaller circle lies completely inside the bigger one, without touching each other. In this case, the sum of the distance between the centers, and the smaller radius, is lesser than a bigger radius. So the smaller one will be inside the bigger one.The second case is the smaller circle is inside the bigger ones, but also touches the circumference of the bigger circle.The third case is, some part of the smaller ...

Read More
Showing 5651–5660 of 5,962 articles
« Prev 1 564 565 566 567 568 597 Next »
Advertisements