C++ Articles

Page 50 of 597

Count number of strings (made of R, G and B) using given combination in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 429 Views

Given three numbers R, G and B and letters ‘R’, ‘G’ and ‘B’ only. The goal is to find the count of possible strings that can be made using at least R Rs, at least G Gs and at least B Bs in it. The numbers R, G and B have sum less than or equal to the length of strings possible.For ExampleInputR = 1, G = 1, B = 1 length=3OutputCount of number of strings (made of R, G and B) using given combination are − 6ExplanationThe possible strings will be : “RGB”, “RBG”, “BRG”, “BGR”, “GRB”, “GBR”. That ...

Read More

Count the number of currency notes needed in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 755 Views

Given an amount in rupees that one has to pay, say pay_rupees and an unlimited amount of banknotes that have value as Rupees_amount_1 and Rupees_amount_2. The goal is to pay pay_rupees using exactly the total number of notes=distribution_total and count the number of type Rupees_amount_1 notes required. If there is no solution to pay then return −1 as answer.For ExampleInputRupees_amount_1 = 1, Rupees_amount_2 = 5, pay_Rupees = 11 distribution_total = 7OutputCount of number of currency notes needed are − 6Explanation6*1 + 5*1 = 11 and notes=6+1=7InputRupees_amount_1 = 2, Rupees_amount_2 = 3, pay_Rupees = 10 distribution_total = 4OutputCount of number of ...

Read More

Construct sum-array with sum of elements in given range in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 635 Views

Given an array arr[ ] containing integers only and an odd number sum. The goal is to create a sum array arr_2[ ] such each arr_2[i] is the sum of previous sum/2 elements of arr[] + arr[i] + next sum/2 elements of arr[]. If sum is 1 then arr_2[i]=arr[i]For ExampleInputarr[] = { 4, 1, 7, 5, 2, 9} sum=3OutputConstruction of sum-array with sum of elements in given range are: 5 12 13 14 16 17 17 9 3ExplanationThe sum array is constructed as: arr_2[0]=arr[0]+arr[1] = 4+1 = 5 arr_2[1]=arr[0]+arr[1]+arr[2] = 4+1+7 = 12 arr_2[2]=arr[1]+arr[2]+arr[3] = 1+7+5 = 13 arr_2[3]=arr[2]+arr[3]+arr[4] = ...

Read More

Count number of trees in a forest in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 568 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

Count the number of common divisors of the given strings in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 349 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 subarrays whose product is divisible by k in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 408 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 subtrees that sum up to a given value x in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 382 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 the nodes in the given tree whose sum of digits of weight is odd in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 288 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 the nodes in the given tree whose weight is a power of two in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 294 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 the nodes of the tree whose weighted string contains a vowel in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 209 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
Showing 491–500 of 5,961 articles
« Prev 1 48 49 50 51 52 597 Next »
Advertisements