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 <iostream> using namespace std; 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; } int main() { int a[] = {1, 2, 3, 9}; int n = sizeof(a) / sizeof(a[0]); cout << countDivisibles(a, n); return 0; } |
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[4] {1, 2, 3, 9}; int n = a.Length; Console.Write(countDivisibles(a, n)); } } // This code is contributed by Smitha. |
PHP
<?php // PHP program to count divisible pairs. function countDivisibles($arr, $n) { $res = 0; // Iterate through all pairs for ($i = 0; $i < $n; $i++) for ($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; } $a = array(1, 2, 3, 9); $n = count($a); echo (countDivisibles($a, $n)); ?> |
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.
Recommended Posts:
- Count pairs in array whose sum is divisible by 4
- Count pairs in array whose sum is divisible by K
- Count number of pairs in array having sum divisible by K | SET 2
- 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]
- Check if an array can be divided into pairs whose sum is divisible by k
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j])
- Count of pairs (x, y) in an array such that x < y
- Count pairs in an array that hold i*arr[i] > j*arr[j]
- Count pairs in a sorted array whose sum is less than x
- Count pairs in an array such that frequency of one is at least value of other
- Count pairs in an array that hold i+j= arr[i]+arr[j]
- Count all prefixes of the given binary array which are divisible by x
- Count elements that are divisible by at-least one element in another array
- Count the number of elements in an array which are divisible by k
- Count pairs in a sorted array whose product is less than k
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.



