Count Nodes in Tree Whose Weight is a Power of Two in C++

Sunidhi Bansal
Updated on 05-Jan-2021 14:58:58

274 Views

Given a binary tree with weights of its nodes. The goal is to find the number of nodes that have weights such that the number is power of two. If weight is 32 then it is 25 so this node will be counted.For ExampleInputThe tree which will be created after inputting the values is given below −OutputCount the nodes in the given tree whose weight is a power of two are: 3Explanationwe are given with the tree node and the weights associated with each node. Now we calculate the power of each and every weight and check whether it can ... Read More

Count Nodes Whose Weight is a Perfect Square in C++

Sunidhi Bansal
Updated on 05-Jan-2021 14:57:18

169 Views

Given a binary tree with weights of its nodes. The goal is to find the number of nodes that have weights such that the number is a perfect square. If weight is 36 then it is 62 so this node will be counted.For ExampleInputThe tree which will be created after inputting the values is given below −OutputCount the nodes whose weight is a perfect square are: 4Explanationwe are given with the tree nodes and the weights associated with each node. Now we check whether the digits of nodes are perfect squares or not.NodeWeightPerfect squareYes/no212111*11yes1819*9yes437Prime numberno3255*5yes810010*10yes9701Not possiblenoInputThe tree which will be ... Read More

Count Nodes of Tree with Vowel in Weighted String in C++

Sunidhi Bansal
Updated on 05-Jan-2021 14:53:08

193 Views

Given a binary tree with weights of its nodes as strings. The goal is to find the number of nodes that have weights such that the string contains a vowel. If weight is ‘aer’ then it has vowels ‘a’ and ‘e’ so the node will be counted.For ExampleInputThe tree which will be created after inputting the values is given below −OutputCount the nodes of the tree whose weighted string contains a vowel are: 5Explanationwe are given with the tree nodes and the string weights associated with each node. Now we check whether the string of nodes contains vowels or not.NodeWeightvowelsyes/no2aeeyes1bcdNo ... Read More

Count Nodes in Tree with Odd Sum of Digits of Weight in C++

Sunidhi Bansal
Updated on 05-Jan-2021 13:56:22

270 Views

Given a binary tree with weights of its nodes. The goal is to find the number of nodes that have weights such that the sum of digits in that weights add up to an odd number. If weight is 12 then the digit sum is 3 which is odd so this node will be counted.For ExampleInputThe tree which will be created after inputting the values is given below −OutputCount of nodes in the given tree whose sum of digits of weight is odd are: 2Explanationwe are given with the tree node and the weights associated with each node. Now we ... Read More

Count Subtrees that Sum Up to a Given Value X in C++

Sunidhi Bansal
Updated on 05-Jan-2021 13:39:31

365 Views

Given a binary tree and a value x as input. The goal is to find all the subtrees of a binary tree that have sum of weights of its nodes equal to x.For ExampleInputx = 14. The tree which will be created after inputting the values is given belowOutputCount of subtrees that sum up to a given value x are: 1Explanationwe are given with a x value as 14. As we can see there is only one leaf node with the values as 14 therefore the count is 1.Inputx = 33. The tree which will be created after inputting the ... Read More

Count Subarrays Whose Product is Divisible by K in C++

Sunidhi Bansal
Updated on 05-Jan-2021 13:36:48

387 Views

Given an array arr[] and an integer k as input. The goal is to find the number of subarrays of arr[] such that the product of elements of that subarray is divisible by k.For ExampleInputarr[] = {2, 1, 5, 8} k=4OutputCount of sub-arrays whose product is divisible by k are: 4ExplanationThe subarrays will be: [ 8 ], [ 5, 8 ], [ 1, 5, 8 ], [ 2, 1, 5, 8 ].Inputarr[] = {7, 1, 9, 7} k=9OutputCount of sub−arrays whose product is divisible by k are: 6ExplanationThe subarrays will be: [ 9 ], [ 9, 7 ], [ 1, ... Read More

Count Squares with Odd Side Length in Chessboard using C++

Sunidhi Bansal
Updated on 05-Jan-2021 13:33:58

865 Views

Given a number size as input as dimension of size*size Chessboard. The goal is to find the number of squares that can be formed inside that board having odd lengths.For ExampleInputsize=3OutputCount of squares with odd side length in Chessboard are: 10ExplanationAll squares will be as shown : and 1 whole square of size 3x3.Inputsize=4OutputCount of squares with odd side length in Chessboard are: 20Explanationthere will be 16, 1X1 squares. And 4, 3X3 squares inside it.Approach used in the below program is as follows −In this approach we will traverse from length of square as 1 to length as size. For ... Read More

Count Subsets That Satisfy Given Condition in C++

Sunidhi Bansal
Updated on 05-Jan-2021 13:32:05

453 Views

Given an array of numbers and an integer x as input. The goal is to find all the subsets of arr[] such that individual elements of that set as well as the sum of them fully divisible by x.For ExampleInputarr[] = {1, 2, 3, 4, 5, 6} x=3OutputCount of subsets that satisfy the given condition :3ExplanationThe subsets will be: [3], [6], [3, 6]Inputarr[] = {1, 2, 3, 4, 5, 6} x=4OutputCount of subsets that satisfy the given condition :1ExplanationThe subsets will be: [4]Approach used in the below program is as follows −In this approach we will count the elements of ... Read More

Count Common Divisors of Given Strings in C++

Sunidhi Bansal
Updated on 05-Jan-2021 13:30:22

329 Views

Given two strings numo and demo as input. The goal is to find the number of common divisors of both the strings. The divisors of a string are found using following technique: If string str has sub1 as its divisor then we can construct str using sub1 by repeating it any number of times till str is generated. Example: str=abcabcabc sub1=abcFor ExampleInputnumo = "abababab" demo = "abababababababab"OutputCount of number of common divisors of the given strings are: 2ExplanationThe strings can be generated using following divisor substrings : “ab”, “abab”Inputnumo = "pqqppqqp" demo = "pqpq"OutputCount of number of common divisors of ... Read More

Count Number of Trees in a Forest using C++

Sunidhi Bansal
Updated on 05-Jan-2021 13:27:47

531 Views

Given vertices of a forest ( collection of trees). The goal is to find the number of trees in that forest. We will do this by running a DFS (depth first search) algorithm on the forest.For ExampleInputedges = { { 1, 3 }, {2, 8}, {2, 6}, {3, 5}, {3, 7}, {4, 8} }OutputCount of number of trees in a forest are: 3ExplanationThe number of trees that are present in the forest are −Approach used in the below program is as follows −In this approach we apply Depth First search algorithm on the graph recursively. We will increment count if ... Read More

Advertisements