Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Count divisible pairs in an array

  • Difficulty Level : Easy
  • Last Updated : 18 Jun, 2021

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

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

 

C++




// CPP program to count divisible pairs.
#include <bits/stdc++.h>
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));
?>

Javascript




<script>
 
// JavaScript program to count divisible pairs.
 
 
function countDivisibles(arr, n) {
    let res = 0;
 
    // Iterate through all pairs
    for (let i = 0; i < n; i++)
        for (let 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;
}
 
 
let a = [1, 2, 3, 9];
let n = a.length;
document.write(countDivisibles(a, n));
 
</script>
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
Recommended Articles
Page :

Start Your Coding Journey Now!