Found 7197 Articles for C++

Find all the patterns of “1(0+)1” in a given string in C++

Arnab Chakraborty
Updated on 01-Nov-2019 06:31:21

289 Views

Suppose a string has patterns like 1(0+)1. Where (0+) indicates non-empty consecutive occurrences of 1s. We have to find all of the patterns. The patterns can overlap. The string is not necessarily a binary string. It can hold digits and lowercase characters only. Suppose the string is like 1101001, then there are two such patterns. 101 and 1001.To solve this problem, we will follow these steps −Iterate through all character c in the stringWhen c is 1, then we iterate till the element is 0When the stream of 0 ends, we will check whether the next character is 1 or ... Read More

Find all strings that match specific pattern in a dictionary in C++

Arnab Chakraborty
Updated on 01-Nov-2019 06:28:22

586 Views

Consider we have a list of strings called dictionary. We have another pattern string. Our task is to find those strings that matches the pattern. Suppose the dictionary is like [“abb”, “xyz”, “aab”, “kmm”], and pattern is “stt”, then the results will be “abb”, and “kmm”. As the pattern has one letter at first, then the two same letters, so it will follow same pattern strings.To solve this problem, we will encode the pattern in such a way that any word from the dictionary, that matches the pattern, will have the same hash like the pattern after encoding. We will ... Read More

Program to print path from root to all nodes in a Complete Binary Tree using C++

Ayush Gupta
Updated on 01-Nov-2019 06:31:57

215 Views

In this tutorial, we will be discussing a program to print the path from the root node of a binary tree to all the other nodes present in the given binary tree.In this program, we will be given a number N, denoting the number of elements present in the binary tree from 1 to N; 1 being the root node of the binary tree. Therefore, our task is to print all the possible paths from the root node to the various other elements present in the binary tree.To solve this program, we know that for given node I, its left ... Read More

Find all possible coordinates of parallelogram in C++

Arnab Chakraborty
Updated on 01-Nov-2019 06:26:57

214 Views

Find the all of the possible coordinates from the given three coordinates to make e parallelogram of a non-zero area. Suppose A, B, C are three given points we can have only three possible situations.AB, AC are sides, and BC is diagonalAB, BC are sides, and AC is diagonalBC, AC are sides, and AB is diagonalSo we can say that only three coordinates are possible, from which we can generate a parallelogram, if three coordinates are given. Since the opposite sides are equal, then AD = BC, and AB = CD, we will calculate the coordinate of the missing points ... Read More

Program to print path from root to a given node in a binary tree using C++

Ayush Gupta
Updated on 01-Nov-2019 06:33:54

285 Views

In this tutorial, we will be discussing a program to print the path from root to a given node in a binary tree.For a given binary tree having distinct nodes, we have to print the complete path to reach a particularly given node from the root node of the binary tree.To solve this problem, we will use recursion. While traversing the binary tree, we will recursively search for the particular element to be found. Also alongside we will be storing the path to reach the element to be searched.Example#include using namespace std; struct Node{    int data;    Node ... Read More

Program to print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime Using C++

Ayush Gupta
Updated on 01-Nov-2019 06:21:09

167 Views

In this tutorial, we will be discussing a program to print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are coprime.In this, we will be given an integer N, we have to print N integers less than 109 such that no two consecutive numbers are coprime but a pair of 3 consecutive integers must be coprime.For example, let us say we have the integer 4. Then the numbers that follow both of the above-provided conditions are6 15 35 14Example#include using namespace std; #define limit 1000000000 #define MAX_PRIME 2000000 #define MAX 1000000 #define I_MAX ... Read More

Find all distinct subsets of a given set in C++

Arnab Chakraborty
Updated on 01-Nov-2019 06:17:40

3K+ Views

Here we will see how to display all distinct subsets of a given set. So if the set is {1, 2, 3}, then the subsets will be {}, {1}, {2}, {3}, {1, 2}, {2, 3}, {1, 3}, {1, 2, 3}. The set of all subsets is called power set. The power set has 2n elements.We will loop through 0 to 2n (excluding), in each iteration we will check whether the ith bit in the current counter is set, then print ith element.Example#include #include using namespace std; void showPowerSet(char *set, int set_length) {    unsigned int size = pow(2, set_length);    for(int counter = 0; counter < size; counter++) {       cout

Program to print nodes in the Top View of Binary Tree using C++

Ayush Gupta
Updated on 01-Nov-2019 06:15:48

230 Views

In this tutorial, we will be discussing a program to print all the nodes that appear in the top view of a given binary tree.For a particular binary tree, a node appears in its top view if it is the very first node at its horizontal distance. Horizontal distance for the left node of a node x is x-1 and for the right node of node x is x+1.To solve this, we will do the level order traversal so that we get the topmost node for a particular level before the other nodes present at that level. Further, we will ... Read More

Find all distinct subset (or subsequence) sums of an array in C++

Arnab Chakraborty
Updated on 01-Nov-2019 06:14:35

452 Views

Suppose we have a set of integers. Find distinct sum, that can be formed from the subset of the given sets and print them in an ascending order. The sum of array elements is small. Consider the array elements are like [1, 2, 3]. Output will be 0, 1, 2, 3, 4, 5, 6. The distinct subsets are {}, {1}, {2}, {3}, {1, 2}, {2, 3}, {1, 3}, {1, 2, 3}, the sum values are 0, 1, 2, 3, 3, 5, 4, 6.To solve this, we will use the dynamic programming approach. When the sum of given element is small, ... Read More

Program to print nodes between two given level numbers of a binary tree using C++

Ayush Gupta
Updated on 01-Nov-2019 06:12:35

162 Views

In this tutorial, we will be discussing a program to print nodes between the two given level numbers of a binary tree.In this, we will be given a low level and a high level for a particular binary tree and we have to print all the elements between the given levels.To solve this we can use queue-based level traversal. While moving through inorder traversal we can have a marking node at the end of each level. Then we can go to each level and print its nodes if the marking node exists between the given levels.Example#include #include using ... Read More

Advertisements