In this tutorial, we will be discussing a program to print the longest common substring.For this we will be given two strings say A and B. We have to print the longest substring common to the two input strings A and B.For example, if we are given with “HelloWorld” and “world book”. Then the longest common substring, in this case, will be the “world”.Example#include #include #include using namespace std; void print_lstring(char* X, char* Y, int m, int n){ int longest[m + 1][n + 1]; int len = 0; int row, col; for (int i = 0; i
Suppose we have a sorted array with distinct positive integers. We have to find all triplets, that forms Geometric progression with integral common ratio. Suppose the array elements are [1, 2, 6, 10, 18, 54], The triplets are (2, 6, 18), and (6, 18, 54), these are forming geometric progression.To solve this, we will start from the second element, and fix every element as middle element, and search for the lesser and greater elements. For middle element arr[j] to be middle of geometric progression, the previous element arr[i] and arr[k] will be like$$\frac{arr[j]}{arr[i]}=\frac{arr[k]}{arr[j]}=r𝑟$$Example#include using namespace std; void getTriplets(int arr[], int ... Read More
In this tutorial, we will be discussing a program to print the path from the root node to all the leaf nodes in a given binary tree.For example, let us say we have the following binary treeIn this binary tree, we have 4 leaf nodes. Therefore we can have 4 paths from the root node to the leaf node.To solve this, we will use the iterative approach. While doing preorder traversal of the binary tree we can store parent pointers in a map. Whenever during traversal we encounter a leaf node we can easily print its path from the root ... Read More
In this tutorial, we will be discussing a program to print prime numbers for a given range of numbers using the C++ Standard Template Library.In this, we will be given two numbers say a and b. The task is to print all the coming prime numbers in this range. For this, we will be using the Sieve of Eratosthenes method by running it as a subroutine. Simultaneously we will be storing all the prime numbers in a vector and finally printing them all.Example#include using namespace std; typedef unsigned long long int unll; vector eratosthemes(unll n){ vector prime_num(n+1, true); ... Read More
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
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
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
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
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
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP