 GeeksforGeeks App
Open App Browser
Continue

# 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]

Given an array arr[] of N integers, the task is to find the count of unordered index pairs (i, j) such that i != j and 0 <=i < j < N and either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i].

Examples:

Input: arr[] = {2, 4}
Output:
(0, 1) is the only index pair possible.

Input: arr[] = {3, 2, 4, 2, 6}
Output:
Possible pairs are (0, 4), (1, 2), (1, 3), (1, 4), (2, 3) and (3, 4).

Approach: The idea is to find the maximum element from the array and use variable count to store the number of unordered pairs, and array freq[] to store the frequency of the elements of the array. Now traverse the array and for each element find the numbers that are divisible by the ith number of the array and are less than or equal to the maximum number in the array. If the number exists in the array then update the variable count.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to find number of unordered pairs``int` `freqPairs(``int` `arr[], ``int` `n)``{` `    ``// Maximum element from the array``    ``int` `max = *(std::max_element(arr, arr + n));` `    ``// Array to store the frequency of each``    ``// element``    ``int` `freq[max + 1] = { 0 };` `    ``// Stores the number of unordered pairs``    ``int` `count = 0;` `    ``// Store the frequency of each element``    ``for` `(``int` `i = 0; i < n; i++)``        ``freq[arr[i]]++;` `    ``// Find the number of unordered pairs``    ``for` `(``int` `i = 0; i < n; i++) {``        ``for` `(``int` `j = 2 * arr[i]; j <= max; j += arr[i]) {` `            ``// If the number j divisible by ith element``            ``// is present in the array``            ``if` `(freq[j] >= 1)``                ``count += freq[j];``        ``}` `        ``// If the ith element of the array``        ``// has frequency more than one``        ``if` `(freq[arr[i]] > 1) {``            ``count += freq[arr[i]] - 1;``            ``freq[arr[i]]--;``        ``}``    ``}` `    ``return` `count;``}` `// Driver code``int` `main()``{` `    ``int` `arr[] = { 3, 2, 4, 2, 6 };``    ``int` `n = (``sizeof``(arr) / ``sizeof``(arr));` `    ``cout << freqPairs(arr, n);` `    ``return` `0;``}`

## Java

 `import` `java.util.Arrays;` `// Java implementation of the approach``class` `GFG``{` `    ``// Function to find number of unordered pairs``    ``static` `int` `freqPairs(``int` `arr[], ``int` `n)``    ``{` `        ``// Maximum element from the array``        ``int` `max = Arrays.stream(arr).max().getAsInt();` `        ``// Array to store the frequency of each``        ``// element``        ``int` `freq[] = ``new` `int``[max + ``1``];` `        ``// Stores the number of unordered pairs``        ``int` `count = ``0``;` `        ``// Store the frequency of each element``        ``for` `(``int` `i = ``0``; i < n; i++)``        ``{``            ``freq[arr[i]]++;``        ``}` `        ``// Find the number of unordered pairs``        ``for` `(``int` `i = ``0``; i < n; i++)``        ``{``            ``for` `(``int` `j = ``2` `* arr[i]; j <= max; j += arr[i])``            ``{` `                ``// If the number j divisible by ith element``                ``// is present in the array``                ``if` `(freq[j] >= ``1``)``                ``{``                    ``count += freq[j];``                ``}``            ``}` `            ``// If the ith element of the array``            ``// has frequency more than one``            ``if` `(freq[arr[i]] > ``1``)``            ``{``                ``count += freq[arr[i]] - ``1``;``                ``freq[arr[i]]--;``            ``}``        ``}` `        ``return` `count;``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `arr[] = {``3``, ``2``, ``4``, ``2``, ``6``};``        ``int` `n = arr.length;` `        ``System.out.println(freqPairs(arr, n));``    ``}``}` `// This code has been contributed by 29AjayKumar`

## Python3

 `# Python 3 implementation of the approach` `# Function to find number of unordered pairs``def` `freqPairs(arr, n):``    ` `    ``# Maximum element from the array``    ``max` `=` `arr[``0``]``    ``for` `i ``in` `range``(``len``(arr)):``        ``if` `arr[i] > ``max``:``            ``max` `=` `arr[i]` `    ``# Array to store the frequency of``    ``# each element``    ``freq ``=` `[``0` `for` `i ``in` `range``(``max` `+` `1``)]` `    ``# Stores the number of unordered pairs``    ``count ``=` `0` `    ``# Store the frequency of each element``    ``for` `i ``in` `range``(n):``        ``freq[arr[i]] ``+``=` `1` `    ``# Find the number of unordered pairs``    ``for` `i ``in` `range``(n):``        ``for` `j ``in` `range``(``2` `*` `arr[i],``                           ``max` `+` `1``, arr[i]):``            ` `            ``# If the number j divisible by ith``            ``# element is present in the array``            ``if` `(freq[j] >``=` `1``):``                ``count ``+``=` `freq[j]` `        ``# If the ith element of the array``        ``# has frequency more than one``        ``if` `(freq[arr[i]] > ``1``):``            ``count ``+``=` `freq[arr[i]] ``-` `1``            ``freq[arr[i]] ``-``=` `1` `    ``return` `count` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ``arr ``=` `[``3``, ``2``, ``4``, ``2``, ``6``]``    ``n ``=` `len``(arr)` `    ``print``(freqPairs(arr, n))` `# This code is contributed by``# Surendra_Gangwar`

## C#

 `// C# implementation of the approach``using` `System;``using` `System.Linq;` `class` `GFG``{` `    ``// Function to find number of unordered pairs``    ``static` `int` `freqPairs(``int` `[]arr, ``int` `n)``    ``{` `        ``// Maximum element from the array``        ``int` `max = arr.Max();` `        ``// Array to store the frequency of each``        ``// element``        ``int` `[]freq = ``new` `int``[max + 1];` `        ``// Stores the number of unordered pairs``        ``int` `count = 0;` `        ``// Store the frequency of each element``        ``for` `(``int` `i = 0; i < n; i++)``        ``{``            ``freq[arr[i]]++;``        ``}` `        ``// Find the number of unordered pairs``        ``for` `(``int` `i = 0; i < n; i++)``        ``{``            ``for` `(``int` `j = 2 * arr[i]; j <= max; j += arr[i])``            ``{` `                ``// If the number j divisible by ith element``                ``// is present in the array``                ``if` `(freq[j] >= 1)``                ``{``                    ``count += freq[j];``                ``}``            ``}` `            ``// If the ith element of the array``            ``// has frequency more than one``            ``if` `(freq[arr[i]] > 1)``            ``{``                ``count += freq[arr[i]] - 1;``                ``freq[arr[i]]--;``            ``}``        ``}` `        ``return` `count;``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main(String []args)``    ``{``        ``int` `[]arr = {3, 2, 4, 2, 6};``        ``int` `n = arr.Length;` `        ``Console.WriteLine(freqPairs(arr, n));``    ``}``}` `// This code has been contributed by Arnab Kundu`

## PHP

 `= 1)``                ``\$count` `+= ``\$freq``[``\$j``];``        ``}` `        ``// If the ith element of the array``        ``// has frequency more than one``        ``if` `(``\$freq``[``\$arr``[``\$i``]] > 1)``        ``{``            ``\$count` `+= ``\$freq``[``\$arr``[``\$i``]] - 1;``            ``\$freq``[``\$arr``[``\$i``]]--;``        ``}``    ``}` `    ``return` `\$count``;``}` `// Driver code``\$arr` `= ``array``(3, 2, 4, 2, 6);``\$n` `= ``count``(``\$arr``);` `echo` `freqPairs(``\$arr``, ``\$n``);` `// This code is contributed by Ryuga``?>`

## Javascript

 ``

Output:

`6`

Time Complexity: O(max*N), where max is the maximum value of the array.
Auxiliary Space: O(max)

My Personal Notes arrow_drop_up