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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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    

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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));
?>

chevron_right


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.



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.