Found 7197 Articles for C++

Find if n can be written as product of k numbers in C++

Arnab Chakraborty
Updated on 01-Nov-2019 09:57:45

170 Views

Suppose we have a number N. We have another number k. We have to check the number can be represented using k numbers or not. Suppose a number 54, and k = 3, then it will print the numbers like [2, 3, 9], if it cannot be represented, then print that.To solve this, we will find all prime factors of N, and store them into a vector, then to find k numbers greater than 1, we check the size of the vector is greater than k or not. If size is less than k, then return -1, otherwise print first ... Read More

Find if array can be divided into two subarrays of equal sum in C++

Arnab Chakraborty
Updated on 01-Nov-2019 09:54:12

177 Views

Suppose we have an array A. We have to check whether we can split the array into two parts, whose sum are equal. Suppose the elements are [6, 1, 3, 2, 5], then [6, 1], and [2, 5] can be two subarrays.This problem can be solved easily by following these rules. We have to find the sum of all elements of the array at first, then for each element of the array, we can calculate the right sum by using total_sum – sum of elements found so far.Example#include #include using namespace std; void displaySubArray(int arr[], int left, int right) {    cout

Find frequency of smallest value in an array in C++

Arnab Chakraborty
Updated on 01-Nov-2019 07:23:53

354 Views

Here we will see how to find the frequency of smallest element in an array. Suppose the array elements are [5, 3, 6, 9, 3, 7, 5, 8, 3, 12, 3, 10], here smallest element is 3, and the frequency of this element is 4. So output is 4.To solve this we will find the smallest element of the list, then we count the occurrences of first numbers, and that will be the result.Example#include using namespace std;    int min_element(int arr[], int n){    int min = arr[0];    for(int i = 1; i

Find Excel column number from column title in C++

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

263 Views

We know that the excel column numbers are alphabetic. It starts from A, and after Z, it will AA, AB, to ZZ, then again AAA, AAB, to ZZZ and so on. So column 1 is A, column 27 is Z. Here we will see how to get the column letter if number of column is given. So if the column number is 80, then it will be CB.Suppose we have a number n, and its value is 28, then we need to take reminder with 26. If the remainder is 0, then the number is 26, 52 and so on. ... Read More

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

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

169 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

326 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

184 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

154 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

325 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

393 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

Advertisements