Found 7347 Articles for C++

Count squares with odd side length in Chessboard in C++

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

757 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

295 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

188 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

356 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

459 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

454 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

276 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

308 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

Count the number of non-increasing subarrays in C++

Sunidhi Bansal
Updated on 05-Jan-2021 07:20:43

254 Views

Given an array arr[] containing positive integers. The goal is to find the number of subarrays of length at least 1 which are non−increasing. If arr[]= {1, 3, 2}, then subarrays will be {1}, {2}, {3}, {3, 2}. Count is 4.For ExampleInputarr[] = {5, 4, 5}OutputCount of number of non-increasing subarrays are: 7ExplanationThe subarrays will be − {5}, {4}, {5}, {5, 4}Inputarr[] = {10, 9, 8, 7}OutputCount of number of non−increasing subarrays are − 10ExplanationThe subarrays will be − {10}, {9}, {8}, {7}, {10, 9}, {9, 8}, {8, 7}, {10, 9, 8}, {9, 8, 7}, {10, 9, 8, 7}Approach used ... Read More

Count the number of holes in an integer in C++

Sunidhi Bansal
Updated on 05-Jan-2021 07:19:18

420 Views

Given an array of holes[10] containing a number of holes in numbers 0 to 9. The goal is to find the number of holes in an integer number given as input. Given − holes[] = { 2, 1, 1, 0, 0, 1, 1, 1, 0 }For ExampleInputnumber = 239143OutputCount the number of holes in an integer are: 3ExplanationWe will count holes given in holes[] 239143 ( 1+0+0+2+0+0 )Inputnumber = 12345OutputCount the number of holes in an integer are: 3ExplanationWe will count holes given in holes[] 12345 ( 1+1+0+0+1)Approach used in the below program is as follows −Simply take each leftmost ... Read More

Advertisements