C++ Articles - Page 279 of 719

Count numbers in a range that are divisible by all array elements in C++

Sunidhi Bansal
Updated on 31-Oct-2020 04:45:19

655 Views

We are provided two numbers START and END to define a range of numbers. And also an array of positive numbers Arr[]. The goal is to find all the numbers that are divisible by all elements of Arr[] and are in the range [START, END] .Method 1 ( Naive Approach )We will do this by traversing numbers from START to END and for each number we will check if the number is divisible by all elements of the array. If yes increment count.Method 2 ( check divisibility by LCM of array elements )We will find the LCM of all array ... Read More

Count numbers having 0 as a digit in C++

Sunidhi Bansal
Updated on 31-Oct-2020 04:41:17

2K+ Views

We are provided a number N. The goal is to find the numbers that have 0 as digit and are in the range [1, N].We will do this by traversing numbers from 10 to N ( no need to check from 1 to 9 ) and for each number we will check each digit using a while loop. If any digit is found as zero increment count and move to next number otherwise reduce the number by 10 to check digits until number is >0.Let’s understand with examples.Input N=11Output Numbers from 1 to N with 0 as digit: 1Explanation Starting from i=10 to ... Read More

Count numbers from range whose prime factors are only 2 and 3 in C++

Sunidhi Bansal
Updated on 31-Oct-2020 04:39:15

776 Views

We are provided two numbers START and END to define a range of numbers. The goal is to find the numbers that have only 2 and 3 as their prime factors and are in the range [START, END].We will do this by traversing numbers from START to END and for each number we will check if the number is divisible by 2 and 3 only. If divisible, divide it and reduce it. If not, break the loop. In the end if the number is reduced to 1 then it has only 2 and 3 as its factors.Let’s understand with examples.Input START=20 ... Read More

Count number of triplets with product equal to given number in C++

Sunidhi Bansal
Updated on 31-Oct-2020 04:36:35

266 Views

We are given an array Arr[] of integers with length n and a number M. The array is only containing positive integers. The goal is to count the triplets of elements of Arr[] which have product equal to M.We will do this by using three for loops. Increment count if arr[x]*arr[y]*arr[z]=M and x!=y!=z. (0

Program to count number of valid triangle triplets in C++

Arnab Chakraborty
Updated on 21-Oct-2020 12:16:17

867 Views

Suppose we have an array of numbers, we have to find the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. So if the input is like [2, 2, 3, 4], then the result will be 3 as there are three triplets [2, 3, 4] using first 2, [2, 3, 4] using second 2, and [2, 2, 3].To solve this, we will follow these steps −ret := 0, n := size of nums, sort numsfor i in range n − 1 down to 0right := i − 1, ... Read More

Program to pruning a given binary tree in C++

Arnab Chakraborty
Updated on 21-Oct-2020 12:10:20

389 Views

Suppose we have a binary tree, where every node's value is either a 0 or a 1. We have to find the same tree where every subtree not containing a 1 has been deleted. So if the tree is like −To solve this, we will follow these steps −Define a recursive method solve(), this will take the node. the method will be like −If node is null, then return nullleft of node := solve(left of node)right of node := solve(right of node)if left of node is null and right of node is also null and node value is 0, then ... Read More

Program to find spreadsheet column number from the column title in C++

Arnab Chakraborty
Updated on 21-Oct-2020 12:02:35

149 Views

Suppose we have a column title of spreadsheet. We know that the spreadsheet 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. So we have to find the corresponding column title from the number. If the input is like 30, it will AD.Example Live Demo#include #include using ... Read More

Program to find spreadsheet column title from the column number in C++

Arnab Chakraborty
Updated on 21-Oct-2020 12:00:42

224 Views

Suppose we have a positive integer value; we have to find its corresponding column title as appear in a spread sheet. So [1 : A], [2 : B], [26 : Z], [27 : AA], [28 : AB] etc.So, if the input is like 29, then the output will be AC.To solve this, we will follow these steps −while n is non-zero, do −n := n − 1res := res + n mod 26 + ASCII of 'A'n := n / 26reverse the array resreturn resLet us see the following implementation to get better understanding −Example Live Demo#include using namespace std; ... Read More

Program to find sum of the deepest nodes in C++

Arnab Chakraborty
Updated on 21-Oct-2020 11:36:28

122 Views

Suppose we have a binary tree; we have to find the sum of values of its deepest leaves. So if the tree is like −Then the output will be 11.To solve this, we will follow these steps −Define a map m, and maxDepthDefine a recursive method solve(), this will take node and level, initially level is 0if node is not present, then returnmaxDepth := max of level and maxDepthincrease m[level] by value of nodesolve(left of node, level + 1)solve(right of node, level + 1)In the main method, setup maxDepth := 0, then solve(root, 0)return m[maxDepth]Let us see the following implementation ... Read More

Program to find sum of the right leaves of a binary tree in C++

Arnab Chakraborty
Updated on 21-Oct-2020 11:33:29

202 Views

Suppose we have a binary tree we have to find the sum of all right leaves in a given binary tree.So, if the input is likethen the output will be 17, as there are two right leaves in the binary tree, with values 7 and 10 respectively.To solve this, we will follow these steps −Define a function dfs(), this will take node, add, if node is null, then −returnif left of node is null and right of node is null and add is non-zero, then −ret := ret + val of nodedfs(left of node, false)dfs(right of node, true)From the main ... Read More

Advertisements