# Count divisible pairs in an array

Given an array, count pairs in the array such that one element of pair divides other.

Examples:

```Input  : arr[] = {1, 2, 3}
Output : 2
The two pairs are (1, 2) and (1, 3)

Input : arr[] = {2, 3, 5, 7}
Output: 0
```

## C++

 `// CPP program to count divisible pairs. ` `#include ` `using` `namespace` `std; ` ` `  `int` `countDivisibles(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `res = 0; ` ` `  `    ``// Iterate through all pairs ` `    ``for` `(``int` `i=0; i

## Java

 `// Java program to count ` `// divisible pairs. ` ` `  `class` `GFG { ` `     `  `// Function returns count ` `// of divisible pairs ` `static` `int` `countDivisibles(``int` `arr[],  ` `                              ``int` `n) ` `{ ` `    ``int` `res = ``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` `(arr[i] % arr[j] == ``0` `||  ` `            ``arr[j] % arr[i] == ``0``)  ` `            ``res++; ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `a[] = ``new` `int``[]{``1``, ``2``, ``3``, ``9``}; ` `    ``int` `n = a.length; ` `    ``System.out.print(countDivisibles(a, n)); ` `} ` `} ` ` `  `// This code is contributed by Smitha. `

## Python3

 `# Python3 program to count  ` `# divisible pairs.  ` ` `  `def` `countDivisibles(arr, n) : ` ` `  `    ``res ``=` `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` `(arr[i] ``%` `arr[j] ``=``=` `0` `or` `            ``arr[j] ``%` `arr[i] ``=``=` `0``) : ` `                ``res``+``=``1` ` `  `    ``return` `res  ` ` `  `# Driver code  ` `if` `__name__``=``=``'__main__'``: ` `    ``a ``=` `[``1``, ``2``, ``3``, ``9``] ` `    ``n ``=` `len``(a)  ` `    ``print``(countDivisibles(a, n) ) ` ` `  `# this code is contributed by  ` `# Smitha Dinesh Semwal     `

## C#

 `// Java program to count ` `// divisible pairs. ` `using` `System; ` ` `  `class` `GFG { ` `     `  `// Function returns count ` `// of divisible pairs ` `static` `int` `countDivisibles(``int` `[]arr,  ` `                              ``int` `n) ` `{ ` `    ``int` `res = 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` `(arr[i] % arr[j] == 0 ||  ` `            ``arr[j] % arr[i] == 0)  ` `            ``res++; ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int``[] a = ``new` `int`` {1, 2, 3, 9}; ` `    ``int` `n = a.Length; ` `    ``Console.Write(countDivisibles(a, n)); ` `} ` `} ` ` `  `// This code is contributed by Smitha. `

## PHP

 ` `

Output:

```4
```

Efficient solution for small ranged numbers.
1) Insert all elements of array in a hash table.
2) Find maximum element in the array.
3) For every array element, search multiples of it (till maximum) in the hash table. If found, increment result.
Different cases like negative numbers and repetitions can also be handled here with slight modifications to the approach.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.