Found 7197 Articles for C++

Find cubic root of a number in C++

Arnab Chakraborty
Updated on 01-Nov-2019 07:07:38

420 Views

Here we will see how to get the cubic root of a number. Suppose a number is say 27, then the cubic root of this number is 3. To solve this problem, we will define our own logic without using some library functions. We will use the binary search approach. We have to follow these steps to solve this problem.Suppose we have threshold value like threshold = 0.000001Start the left value as 0, and right value as numbercalculate mid := (left + right)/2if the absolute value of (number – mid3) is less than threshold, then return mid as answerif mid3 ... Read More

Find count of digits in a number that divide the number in C++

Arnab Chakraborty
Updated on 01-Nov-2019 07:04:38

281 Views

Suppose a number is given. We have to count number of digits of the number which evenly divides the number. Suppose the number is 1012, the result is 3. There are three digits 1, 1, and 2 that evenly divide 1012.To solve this, we will find each digit of the number, using modulus operation, and check whether the number is divisible by that digit or not, if divisible, then increase counter. If the digit is 0, then ignore that digit.Example#include using namespace std;    int countDivDigit(int num) {    int count = 0;    int temp = num;    while(temp){ ... Read More

Program to print the DFS traversal step-wise using C++

Ayush Gupta
Updated on 01-Nov-2019 07:05:15

445 Views

In this tutorial, we will be discussing a program to print the steps of the traversal using Depth First Search in a given binary tree.This would include every step that occurs in the depth-first search including the backtracking procedure as well.During DFS, we will be traversing each node and simultaneously storing the parent node and the edge used. During the traversal, if the adjacent edge has been visited, then the exact node can be printed as a step in the depth-first search.Example#include using namespace std; const int N = 1000; vector adj[N]; //printing the steps in DFS traversal void ... Read More

Find closest value for every element in array in C++

Arnab Chakraborty
Updated on 01-Nov-2019 07:02:04

376 Views

Here we will see how to find the closest value for every element in an array. If an element x has next element that is larger than it, and also present in the array, then that will be the greater value of that element. If the element is not present, then return -1. Suppose the array elements are [10, 5, 11, 6, 20, 12], then the greater elements are [11, 6, 12, 10, -1, 20]. As 20 has not greater value in the array, then print -1.To solve this, we will use the set in C++ STL. The set is ... Read More

Program to print the first shortest root to leaf path in a Binary Tree using C++

Ayush Gupta
Updated on 01-Nov-2019 07:01:16

286 Views

In this tutorial, we will be discussing a program to print the first shortest root to leaf path in a Binary Tree.In this, we will be given a binary tree with distinct values, and we have to find the shortest path from the root node to the leaf node in a given binary tree.To solve this, we can use a queue to perform level order traversal in a binary tree and store the nodes in the shortest path. For this, once we reach the leftmost node for the final level, we retrieve the values from the queue and print the ... Read More

Program to print the longest common substring using C++

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

654 Views

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

Program to print root to leaf paths without using recursion using C++

Ayush Gupta
Updated on 01-Nov-2019 06:42:29

179 Views

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

Find closest greater value for every element in array in C++

Arnab Chakraborty
Updated on 01-Nov-2019 06:58:39

310 Views

Here we will see how to find the closest greater value for every element in an array. If an element x has next element that is larger than it, and also present in the array, then that will be the greater value of that element. If the element is not present, then return -1. Suppose the array elements are [10, 5, 11, 6, 20, 12], then the greater elements are [11, 6, 12, 10, -1, 20]. As 20 has not greater value in the array, then print -1.To solve this, we will use the set in C++ STL. The set ... Read More

Program to print prime numbers in a given range using C++ STL

Ayush Gupta
Updated on 01-Nov-2019 06:36:49

285 Views

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

Find all triplets in a sorted array that forms Geometric Progression in C++

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

510 Views

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

Advertisements