C++ Articles - Page 294 of 719

Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i] in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:49:03

1K+ Views

We are given with an array arr[] of N elements. The goal is to find the count of all valid pairs of indexes (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i] and i!=j.We will do this by traversing the array arr[] using two for loops for each number of pair and check if arr[i]%arr[j]==0 or arr[j]%arr[i]==0 when i!=j. If true increment count of pairs.Let’s understand with examples.Input − Arr[]= { 2, 4, 3, 6 } N=4Output − Count of valid pairs − 3Explanation − Valid pairs are −Arr[0] & Arr[1] → (2, ... Read More

Count the number of possible triangles in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:47:14

851 Views

We are given an array which contains the length of sides of triangles. The goal is to find the number of possible triangles that can be made by taking any three sides from that array.We will do this by checking if the sum of any two is always > third side. If yes these three sides can make a triangle. Increment count of possible triangles that can be made.Let’s understand with examples.Input − arr[]= {1, 2, 4, 5}Output − Count of possible triangles − 1Explanation − Sides (2, 4, 5) can only make a triangle as 2+4>5 & 4+5>2 & ... Read More

Count the numbers divisible by ‘M’ in a given range in C++

Sunidhi Bansal
Updated on 16-Sep-2020 10:03:55

2K+ Views

We are given three numbers A,B and M. A and B define the range [A,B] of numbers.The goal is to count numbers between A and B that are divisible by M.We will start from i=A till first multiple of M. Increment count if i%M=0. Now increment i till i

Count Triplets such that one of the numbers can be written as sum of the other two in C++

Ravi Ranjan
Updated on 01-Jul-2025 15:32:12

1K+ Views

In this article, We are given an array arr[] of integers with length n. Our task is to count the number of triplets such that the sum of any two numbers is equal to the third number. Example Here is an example of counting the triplets whose sum of any two numbers equals the third one: Input: arr[]= {1, 2, 2, 3, 4} Output: 4 The explanation of the above example is as follows: Triplet 1: (1, 2, 3) => 1+2=3 Triplet 2: (1, 2, 3) => 1+2=3 Triplet ... Read More

Counting cross lines in an array in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:34:55

260 Views

We are given with an array of distinct elements that are unsorted. The goal is to find the cross lines after the array is sorted. Cross lines are counted as shown below −Arr[]={ 1, 2, 4, 3, 5 } There are 3 cross lines as shown belowArr[]= { 1, 2, 3, 4, 5 }. There are no cross lines as the array is already sorted.We will count the cross lines using insertion sort in which an element from right is added to sorted elements on its left. Each time the element is added in the sorted part, increment count as ... Read More

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

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

393 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

429 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

342 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

610 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

411 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

Advertisements