Found 7347 Articles for C++

Check for possible path in 2D matrix in C++

Arnab Chakraborty
Updated on 21-Oct-2019 14:25:13

481 Views

Consider we have a 2D array. We have to find if we can get a path from topleft corner to bottom-right corner. The matrix is filled with 0s and 1s. 0 indicates open area, 1 indicates blockage. Note that the top-left corner will always be 1.Suppose a matrix is like below −0001010011000101000000100One path is marked as green, there are some other paths also. So the program will return true, if there is a path, otherwise false.We will solve this problem, by changing all accessible node to -1, First change the value of starting point to -1, then get next value ... Read More

Check for Palindrome after every character replacement Query in C++

Arnab Chakraborty
Updated on 21-Oct-2019 14:21:21

141 Views

Consider we have a string and some queries in set Q. Each query contains a pair of integers i and j. and another character c. We have to replace characters at index i and j with the new character c. And tell if the string is palindrome or not. Suppose a string is like “AXCDCMP”, if we use one query like (1, 5, B), then the string will be “ABCDCBP”, then another query like (0, 6, A), then it will be “ABCDCBA”, this is palindrome.We have to create one query using indices i, j, then replace the characters present at ... Read More

Check for Majority Element in a sorted array in C++

Arnab Chakraborty
Updated on 21-Oct-2019 14:18:48

139 Views

Suppose we have an array; we have to check whether given number x is the majority element of that array or not. The array is sorted. One element is said to be majority element, when it appears n/2 times in the array. Suppose an array is like {1, 2, 3, 3, 3, 3, 6}, x = 3, here the answer is true as 3 is the majority element of the array. There are four 3s. The size of the array is 7, so we can see 4 > 7/2.We can count the occurrences of x in the array, and if ... Read More

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

Arnab Chakraborty
Updated on 21-Oct-2019 14:17:00

77 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
Updated on 21-Oct-2019 14:12:36

71 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

Check for balanced parentheses in an expression in C++

Arnab Chakraborty
Updated on 21-Oct-2019 14:09:19

3K+ Views

Suppose we have an expression. The expression has some parentheses; we have to check the parentheses are balanced or not. The order of the parentheses are (), {} and []. Suppose there are two strings. “()[(){()}]” this is valid, but “{[}]” is invalid.The task is simple; we will use stack to do this. We should follow these steps to get the solution −Traverse through the expression until it has exhaustedif the current character is opening bracket like (, { or [, then push into stackif the current character is closing bracket like ), } or ], then pop from stack, ... Read More

Check for an array element that is coprime with all others in C++

Arnab Chakraborty
Updated on 21-Oct-2019 14:03:30

244 Views

Suppose we have an array A[] of positive integers, where 2

Floor and Ceil from a BST in C++

Arnab Chakraborty
Updated on 21-Oct-2019 13:56:29

171 Views

Here we will see, how to find the Floor and Ceiling value from BST. For example, if we want to make a memory management system, where free nodes are arranged in BST. Find best fit for the input request. Suppose we are moving down the tree with smallest data larger than the key value, then there are three possible cases.Root is the key. Then root value is the ceiling valueIf root data < key, then the ceiling value will not be at the left subtree, then proceed to right subtree, and reduce the problem domainIf root data > key, then ... Read More

Finding LCM of more than two (or array) numbers without using GCD in C++

Arnab Chakraborty
Updated on 21-Oct-2019 13:52:20

997 Views

We have an array A, we have to find the LCM of all elements without using the GCD operation. If the array is like {4, 6, 12, 24, 30}, then the LCM will be 120.The LCM can be calculated easily for two numbers. We have to follow this algorithm to get the LCM.getLCM(a, b) −begin    if a > b, then m := a, otherwise m := b       while true do          if m is divisible by both a and b, then return m             m := m + ... Read More

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

Arnab Chakraborty
Updated on 21-Oct-2019 13:49:21

54 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

Advertisements