Found 26504 Articles for Server Side Programming

Count subtrees that sum up to a given value x in C++

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

334 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

344 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 in C++

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

836 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 the given condition in C++

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

408 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 the number of common divisors of the given strings in C++

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

298 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 in C++

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

502 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

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

Sunidhi Bansal
Updated on 05-Jan-2021 07:30:09

591 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 the number of currency notes needed in C++

Sunidhi Bansal
Updated on 05-Jan-2021 07:28:24

692 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

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

Sunidhi Bansal
Updated on 05-Jan-2021 07:26:32

381 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 number of subsets having a particular XOR value in C++

Sunidhi Bansal
Updated on 05-Jan-2021 07:25:01

416 Views

Given an array arr[ ] containing positive integers and a value match. The goal is to find the subsets of arr[] that contain elements that have XOR = match.For ExampleInputarr[] = {4, 2, 8, 10} match=12OutputCount of number of subsets having a particular XOR value are: 2ExplanationSubsets of arr with XOR of elements as 0 are − [ 4, 8 ], [4, 2, 10]Inputarr[] = {3, 5, 2, 7} match=5OutputCount of number of subsets having a particular XOR value are− 2Explanationubsets of arr with XOR of elements as 0 are− [ 5 ], [2, 7]Approach used in the below program ... Read More

Advertisements