Found 7197 Articles for C++

Find maximum level product in Binary Tree in C++

Arnab Chakraborty
Updated on 19-Dec-2019 10:07:25

153 Views

Suppose, one binary tree is given. It has positive and negative nodes. We have to find the maximum product at each level of it.Consider this is the tree, so the product of level 0 is 4, product of level 1 is 2 * -5 = -10, and product of level 2 is -1 * 3 * -2 * 6 = 36. So this is the maximum one.To solve this, we will perform the level order traversal of the tree, during traversal, process doing the nodes of different levels separately. Then get the maximum product.Example Live Demo#include #include using namespace std; class ... Read More

Find longest sequence of 1’s in binary representation with one flip in C++

Arnab Chakraborty
Updated on 19-Dec-2019 10:04:04

2K+ Views

Suppose we have one integer n. Inside that, we can make the one-bit flip to generate the longest sequence of 1s. Suppose the number is 13, so binary representation is 1101. If we make a one-bit flip as make 0 to 1, it will be 1111. This is the longest sequence of 1sTo solve this problem, we will walk through the bits of a given number. We will keep track of the current 1’s sequence length, and the previous 1’s sequence length. When a zero has found, then update the previous length. So if the next bit is 1, then ... Read More

Program to make a histogram of an array in C++

Ayush Gupta
Updated on 19-Dec-2019 10:05:50

3K+ Views

In this tutorial, we will be discussing a program to make a histogram by the data given inside an array.For this, we will be provided with integer values inside an array. Our task is to plot a histogram keeping the value of both coordinates x and y equal to the value provided in the array.Example Live Demo#include using namespace std; void make_histogram(int arr[], int n){    int maxEle = *max_element(arr, arr + n);    for (int i = maxEle; i >= 0; i--) {       cout.width(2);       cout

Find Length of a Linked List (Iterative and Recursive) in C++

Arnab Chakraborty
Updated on 19-Dec-2019 10:01:36

742 Views

Here we will see how to find the length of a linked list using iterative and recursive approaches. If the head pointer is given, we have to follow these steps to get the length.For iterative approach −Take the head of the list, until the current pointer is not null, go to the next node and increase the count.For recursive approach −Pass head as an argument, the base condition is when the argument is null, then return 0, otherwise recursively enter into the list and send the next node from the current node, return 1 + length of the sublistExample Live Demo#include ... Read More

Program to invert bits of a number Efficiently in C++

Ayush Gupta
Updated on 19-Dec-2019 10:02:48

387 Views

In this tutorial, we will be discussing a program to invert bits of a number efficiently.For this we will be given with a non-negative number. Our task is to convert the number in the binary format, invert the binary bits of the number. And then finally print the decimal equivalent of the number.Example Live Demo#include using namespace std; //inverting bits of number int invert_bit(int n){    int x = log2(n) ;    int m = 1

Find last k digits in product of an array numbers in C++

Arnab Chakraborty
Updated on 19-Dec-2019 09:58:48

163 Views

Suppose we have an array of n elements called A. We have another number k. Our task is to find last k digits of the product of elements in the array A. Suppose the A = [15, 22, 13, 19, 17], then the product is 1385670, the last k = 3 digits are 670.To solve this problem, we will multiply the numbers under modulo 10k.Example Live Demo#include #include using namespace std; int displayLastKNumbers(int array[], int n, int k) {    int mod = (int)pow(10, k);    int mul = array[0] % mod;    for (int i = 1; i < n; ... Read More

Find largest d in array such that a + b + c = d in C++

Arnab Chakraborty
Updated on 19-Dec-2019 09:56:32

156 Views

Suppose we have a set of integers. We have to find a number ‘d’, where d = a + b + c, and we have to maximize (a + b + c), all a, b, c, and d are present in the set. The set will hold at least one element, and at max 1000 elements. Each element will be a finite number. If the set is {2, 3, 5, 7, 12}, then 12 is largest d. this can be represented by 2 + 3 + 7To solve this problem, we can use the hashing technique. We will store the ... Read More

Find k numbers which are powers of 2 and have sum N in C++

Arnab Chakraborty
Updated on 19-Dec-2019 09:53:04

191 Views

Suppose we have two numbers N and K. The task is to print K numbers, which are the power of 2 and their sum is N. If it is not possible, then return -1. Suppose N = 9 and K = 4, then the output will be 4 2 2 1, whose sum is 9, and a number of elements is 4, and each of them is a power of 2.We have to follow these steps to solve this problem −If k is less than the number of set bits in N or more than the number N, then return ... Read More

Find Intersection of all Intervals in C++

Arnab Chakraborty
Updated on 19-Dec-2019 09:50:23

852 Views

Suppose, we have N intervals in the form {L, R}, the L is the starting time, and R is the ending time. We have to find an intersection of all intervals. An intersection is an interval that lies within all of the given intervals. If no such found, return -1. For example, if the intervals are like [{1, 6}, {2, 8}, {3, 10}, {5, 8}, The output interval is {5, 6}To solve this problem, we will follow these steps −Consider the first interval is the final intervalStarting from the second interval, try searching for the intersection. Two cases can be ... Read More

Find indices of all occurrence of one string in other in C++

Arnab Chakraborty
Updated on 19-Dec-2019 09:42:14

3K+ Views

Suppose we have string str, and another substring sub_str, we have to find the indices for all occurrences of the sub_str in str. Suppose the str is “aabbababaabbbabbaaabba”, and sub_str is “abb”, then the indices will be 1 9 13 18.To solve this problem, we can use the substr() function in C++ STL. This function takes the initial position from where it will start checking, and the length of the substring, if that is the same as the sub_str, then returns the position.Example Live Demo#include using namespace std; void substrPosition(string str, string sub_str) {    bool flag = false;    for ... Read More

Advertisements