Found 7347 Articles for C++

Count total divisors of A or B in a given range in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:26:38

271 Views

We are given four integers L, R, A and B. The goal is to find the count of numbers in range [L, R] that fully divide either A or B or both.We will do this by traversing from L to R and for each number if number%A==0 or number%B==0 then increment count of divisors.Let’s understand with examples.Input − L=10, R=15, A=4, B=3Output − Count of divisors of A or B − 2Explanation −Number 12 is fully divisible by 3 and 4. Number 15 is fully divisible by 3 only. Total divisors=2Input − L=20, R=30, A=17, B=19Output − Count of divisors ... Read More

Count the triplets such that A[i] < B[j] < C[k] in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:22:43

315 Views

We are given with three arrays A[], B[] and C[]. The goal is to find all triplets of elements of these arrays such that A[i]

Count ways of choosing a pair with maximum difference in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:20:01

238 Views

We are given with an array of numbers Arr[]. The goal is to count the number of pairs whose difference is equal to the maximum difference of all possible pairs. Count pairs (i!=j) and arr[x]- arr[y] is maximum possible.We will do this by first finding the maximum difference where (i!=j). And store as maxdiff. Then count all those pairs that have difference=maxdiff.Let’s understand with examples.Input − arr[]= { 1, 2, 3, 2, 4, 1, 5 }Output − No. of ways of choosing pair with maximum difference − 2Explanation −Here minimum no. is 1 and maximum number is 5, maximum difference ... Read More

Count valid pairs in the array satisfying given conditions in C++

Sunidhi Bansal
Updated on 16-Sep-2020 10:00:45

432 Views

We are given with an array arr[] of N elements. The goal is to find the count of all valid pairs (Arr[i],Arr[j]) that follow certain conditions. Pairs Arr[i],Arr[j] invalid if −Arr[i]==Arr[j]Arr[i]+Arr[j] is eveni+j

Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two in C++

Sunidhi Bansal
Updated on 29-Aug-2020 09:35:10

308 Views

We are given with an array of N elements. The goal is to find the count of all pairs (Arr[i], Arr[j]) which have a sum which is a perfect square such that i!=j. That is Arr[i]+Arr[j] is a perfect square.We will do this by calculating the sum of pairs and check if the square root of that sum is equal to the floor value of the square root. sqrt(Arr[i]+Arr[j])-floor( sqrt(Arr[i]+Arr[j] )==0.Let’s understand with examples.Input − Arr[]= { 4, 3, 2, 1, 2, 4 } N=6Output − Count of pairs with sum as perfect square − 2Explanation −Arr[1]+Arr[3]=4, sqrt(4)-floor(4)=0 4 is ... Read More

Count of pairs in an array whose sum is a perfect square in C++

Sunidhi Bansal
Updated on 29-Aug-2020 09:22:33

518 Views

We are given with an array of N elements. The goal is to find the count of all pairs (Arr[i], Arr[j]) which have a sum which is a perfect square such that i!=j. That is Arr[i]+Arr[j] is a perfect square.We will do this by calculating the sum of pairs and check if the square root of that sum is equal to the floor value of the square root. sqrt(Arr[i]+Arr[j])-floor( sqrt(Arr[i]+Arr[j] )==0.Let’s understand with examples.Input − Arr[]= { 4, 3, 2, 1, 2, 4 } N=6Output − Count of pairs with sum as perfect square − 2Explanation −Arr[1]+Arr[3]=4, sqrt(4)-floor(4)=0 4 is ... Read More

Count ways to form minimum product triplets in C++

Sunidhi Bansal
Updated on 29-Aug-2020 09:19:36

134 Views

We are given with an array of numbers Arr[]. The goal is count the number of triplets whose product is equal to the smallest product of all possible triplets.Count triplets if (i

Counting numbers whose difference from reverse is a product of k in C++

Sunidhi Bansal
Updated on 29-Aug-2020 09:11:19

68 Views

We are given a range [l,r] and a number k. The goal is to find all the numbers between l and r (l

Counting pairs when a person can form pair with at most one in C++

Sunidhi Bansal
Updated on 29-Aug-2020 09:08:11

196 Views

We are given with N no. of participants in a coding competition. The goal is to find the no. of pairs that are possible when a person can pair with at most one other person. So a pair has at most 2 participants. The participants are allowed to take part alone also.We can solve this using recurrence where pairs=count=1 when n=0 or 1 ( only one person left )if person remains single n reduced to n-1now for remaining pairing people left = n-2count=makePairs(p-1) + (p-1)*makePairs(p-2);Let’s understand with examples.Input − persons=3Output − Count of ways to make pair − 4Explanation −If ... Read More

Count of index pairs with equal elements in an array in C++

Sunidhi Bansal
Updated on 29-Aug-2020 09:05:28

338 Views

We are given with an array of N elements. The goal is to find the index pairs (i, j) which have the same element value such that i!=j. i.e, Arr[i]=Arr[j] and i!=j. This is used to make pairs of gloves of equal size. Out of N gloves only paired gloves are useful to sell.We will do this by running two loops with 0 -1. Total pairs=2Approach used in the below program is as followsWe take an integer array Arr[] initialized with random numbers for size of gloves > 0.Take a variable n which stores the length of Arr[].Function countPairs(int arr[], ... Read More

Advertisements