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

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

342 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

833 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

403 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

293 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

499 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

588 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

689 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

378 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

412 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

386 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

Advertisements