Found 7347 Articles for C++

Find elements which are present in first array and not in second in C++

Arnab Chakraborty
Updated on 01-Nov-2019 07:19:55

115 Views

Suppose we have two arrays A and B. There are few elements. We have to find those elements that are present in set A, but not in set B. If we think that situation, and consider A and B as set, then this is basically set division operation. The set difference between A and B will return those elements.Example#include #include #include #include using namespace std; void setDiffResults(int A[], int B[], int An, int Bn) {    sort(A, A + An);    sort(B, B + Bn);    vector res(An);    vector::iterator it;    vector::iterator it_res = set_difference(A, A + An, B ... Read More

Find elements of array using XOR of consecutive elements in C++

Arnab Chakraborty
Updated on 01-Nov-2019 07:16:08

259 Views

Consider we have to find a list of n elements. But we have the XOR value of two consecutive elements of the actual array. Also the first element of the actual is given. So if the array elements are a, b, c, d, e, f, then the given array will be a^b, b^c, c^d, d^e and e^f.As the first number is given, named a, that can help us to find all numbers. If we want to find the second element of the actual array, then we have to perform b = a ^ arr[i], for second element c = b ... Read More

Find duplicates under given constraints in C++

Arnab Chakraborty
Updated on 01-Nov-2019 07:13:57

125 Views

Suppose we have a list with 6 different numbers. Only one number is repeated five times. So there are total 10 elements in the array. Find duplicate numbers using only two comparisons. If the list is like [1, 2, 3, 4, 4, 4, 4, 4, 5, 6], so output is 4.As there are only 10 numbers, then for any type of duplicate numbers, the range of numbers will be placed from index 3 to 5. By checking these indices, we can find the result.Example#include using namespace std; int getDuplicate(int array[]) {    if (array[3] == array[4])       return array[3];    else if (array[4] == array[5])       return array[4];    else       return array[5]; } int main() {    int a[] = {1, 2, 3, 4, 4, 4, 4, 4, 5, 6};    cout

Program to print the nodes at odd levels of a tree using C++

Ayush Gupta
Updated on 01-Nov-2019 07:13:41

110 Views

In this tutorial, we will be discussing a program to print the nodes present at the odd levels of a given binary tree.In this program, the level for the root node is considered as 1 and simultaneously the alternative level is the next odd level.For example, let us say we are given with the following binary treeThen the nodes at the odd levels of this binary tree will be 1, 4, 5, 6.Example#include using namespace std; struct Node {    int data;    Node* left, *right; }; //printing the nodes at odd levels void print_onodes(Node *root, bool is_odd = ... Read More

Find duplicates in O(n) time and O(1) extra space - Set 1 in C++

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

200 Views

Suppose we have a list of numbers from 0 to n-1. A number can be repeated as many as possible number of times. We have to find the repeating numbers without taking any extra space. If the value of n = 7, and list is like [5, 2, 3, 5, 1, 6, 2, 3, 4, 5]. The answer will be 5, 2, 3.To solve this, we have to follow these steps −for each element e in the list, do the following steps −sign := A[absolute value of e]if the sign is positive, then make it negativeOtherwise it is a repetition.Example#include ... Read More

Program to print the longest leaf to leaf path in a Binary tree using C++

Ayush Gupta
Updated on 01-Nov-2019 07:10:17

293 Views

In this tutorial, we will be discussing a program to print the longest path that exists from a leaf node to another leaf node in a given binary tree.In other words, we have to print all the nodes that appear in the diameter of the Binary tree. Here, diameter (or width) for a particular binary tree is defined as the number of nodes in the longest path from one end node to another.To solve this, we calculate the diameter of the binary tree using the height function. Then we find the longest path in the left portion of the binary ... Read More

Find cubic root of a number in C++

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

319 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

204 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

369 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

316 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

Advertisements