## Introduction

In this article, we will discuss the problem to count divisible pairs in an array and briefly discuss the approach to be used and its time and space complexity. Before discussing count divisible pairs in an array problem, letâ€™s first discuss what an array means.

**Array: **It is a linear data structure consisting of a collection of values called elements of an array, stored in a contiguous memory location, and can be accessed using array indexes.

In this problem, we are given an array of any size(provided by the user), and our task is to count division pairs in an array. You may be wonderingðŸ¤” what this division pair is. Here is the answer ðŸ˜ƒðŸ‘‰

**Division pair:** If one element of a pair divides another element, then such a pair is called a division pair.

**Example:**

Recommended topic, __kth largest element in an array____ __and __Euclid GCD Algorithm__

## Native Approach

Using brute force, iterating through each element of an array and checking if pair (â€˜iâ€™,â€™jâ€™) are such that â€˜a[i]â€™ % â€˜a[j]â€™ = 0 or not.

### Algorithm

- Take array size as input â€˜nâ€™.
- Create an array of size â€˜nâ€™, lets say, â€˜a[n]â€™.
- Take a temporary variable â€˜countâ€™ for counting division pairs.
- Start a for loop from â€˜iâ€™ = 0 to â€˜n-1â€™.
- Inside this loop, start another for loop from â€˜jâ€™ = â€˜i+1â€™ to â€˜n-1â€™.
- Inside these loop check if â€˜a[i]â€™ % â€˜a[j]â€™ = 0 or â€˜a[j]â€™ % â€˜a[i]â€™ = 0.
- If the condition is true, increment the â€˜countâ€™ variable.
- Else continue.
- Return â€˜countâ€™.
- Print the result.

### Code in C++ ðŸ’»

```
/*
Program to count divisible pairs in an array.
*/
#include <iostream>
using namespace std;
int countPairs(int a[], int n)
{
int count = 0;
// Iterate through all pairs
for (int i=0; i<n; i++)
{
for (int j=i+1; j<n; j++)
{
// Increment count if one divides other
if (a[i] % a[j] == 0 || a[j] % a[i] == 0)
count++;
}
}
return count;
}
int main()
{
int n;
cout<<"Enter array size : ";
cin>>n;
int a[n];
cout<<"\nEnter array elements : ";
for(int i=0;i<n;i++)
cin>>a[i];
cout << countPairs(a, n);
return 0;
}
```

### Code in Java ðŸ’»

```
/*
Java program to count divisible pairs in an array.
*/
import java.util.Scanner;
class CodingNinjas {
static int countPair(int arr[],int n)
{
int count = 0;
// Iterate through all pairs
for (int i=0; i<n; i++)
{
for (int j=i+1; j<n; j++)
{
// Increment count if one divides other
if (a[i] % a[j] == 0 || a[j] % a[i] == 0)
count++;
}
}
return count;
}
// Driver Code
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter array size : ");
n=sc.nextInt();
int a[] = new int[n]
System.out.println("Enter array elements : ");
for(int i=0; i<n; i++)
{
a[i] = sc.nextInt();
}
System.out.print(countPairs(a, n));
}
}
```

### Code in Python ðŸ’»

```
# Python3 program to count divisible pairs in an array.
def countPair(a, n) :
count = 0
# Iterate through all pairs
for i in range(0, n) :
for j in range(i+1, n) :
# Increment count if one divides other
if (a[i] % a[j] == 0 or a[j] % a[i] == 0) :
count+=1
return count
# Driver code
if __name__=='__main__':
n=int(input("Enter array size : "))
a=list(map(int, input("Enter array elements : ").strip().split()))
print(countPair(a, n))
```

### Output

### Time and Space Complexity

We have used two loops in the program, one is a single for loop iterating from 0 to â€˜Nâ€™, This will** ****take O(N) time complexity****.**

Another loop is a nested loop used to count division pairs in an array, this loop iterates from 0 to â€˜Nâ€™ and â€˜i + 1â€™ to â€˜Nâ€™,** this will take O(N*N) time complexity.**

**ï¿½ï¿½ï¿½Total time complexity = O(N) + O(N*N) = O(N*N)**

**ðŸŽ€Space Complexity = O(N), **for an array of size N.

Although the program is simple, but it is not an efficient solution for this problem, letâ€™s discuss an efficient approach, that is by hashing.ðŸ˜‰