
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count divisible pairs in an array in C++
We are given an array of any size containing integer elements and the task is to calculate the count of pairs in an array such that one element of a pair divides another element of a pair.
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
For Example
Input − int arr[] = {1, 2, 3, 6} Output − count is 4
Explanation − (1,2), (1,3), (1,6) and (3,6) are the pairs in which one element of a pair divides another as 1 can divide any number and also 3 divides 6. So the count is 4.
Input − int arr[] = {2, 5, 10} Output − count is 2
Explanation − (2, 10) and (5,10) are the pairs in which one element of a pair divides another as 2 can divide 10 and also 5 can divide 10. So the count is 2.
Approach used in the below program is as follows
Create an array let’s say, arr[]
Calculate the length of an array using the length() function that will return an integer value as per the elements in an array.
Take a temporary variable that will store the count of elements present only in an array.
Start loop for with i to 0 and i less than size of an array
Inside the loop start another loop with j to i+1 till j less than size
Inside the loop check if arr[i] % arr[j] = 0 or arr[j] % arr[i] = 0 then increment the count
Return the count
Print the result.
Example
#include <iostream> using namespace std; int divisibles(int a[], int size){ int result = 0; // Iterating through all pairs for (int i=0; i<size; i++){ for (int j=i+1; j<size; j++){ if (a[i] % a[j] == 0 || a[j] % a[i] == 0){ result++; } } } return result; } int main(){ int a[] = {1, 4, 7, 8, 9}; int size = sizeof(a) / sizeof(a[0]); cout <<"count is " <<divisibles(a, size); return 0; }
Output
If we run the above code we will get the following output −
count is 5
- Related Articles
- Count pairs in array whose sum is divisible by K in C++
- Count pairs in array whose sum is divisible by 4 in C++
- Program to count nice pairs in an array in Python
- Count of index pairs with equal elements in an array in C++
- Count all pairs of an array which differ in K bits in C++
- Count of pairs in an array that have consecutive numbers using JavaScript
- Count pairs whose products exist in array in C++
- Count of pairs in an array whose sum is a perfect square in C++
- Count of pairs (x, y) in an array such that x < y in C++
- Count the number of elements in an array which are divisible by k in C++
- Count pairs in an array such that both elements has equal set bits in C++
- Count pairs in an array that hold i*arr[i] > j*arr[j] in C++
- Count pairs in an array such that at least one element is prime in C++
- Check if an array can be divided into pairs whose sum is divisible by k in Python
- Count valid pairs in the array satisfying given conditions in C++
