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]
Last Updated :
30 Nov, 2022
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: 1
(0, 1) is the only index pair possible.
Input: arr[] = {3, 2, 4, 2, 6}
Output: 6
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++
#include <bits/stdc++.h>
using namespace std;
int freqPairs( int arr[], int n)
{
int max = *(std::max_element(arr, arr + n));
int freq[max + 1] = { 0 };
int count = 0;
for ( int i = 0; i < n; i++)
freq[arr[i]]++;
for ( int i = 0; i < n; i++) {
for ( int j = 2 * arr[i]; j <= max; j += arr[i]) {
if (freq[j] >= 1)
count += freq[j];
}
if (freq[arr[i]] > 1) {
count += freq[arr[i]] - 1;
freq[arr[i]]--;
}
}
return count;
}
int main()
{
int arr[] = { 3, 2, 4, 2, 6 };
int n = ( sizeof (arr) / sizeof (arr[0]));
cout << freqPairs(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static int freqPairs( int arr[], int n)
{
int max = Arrays.stream(arr).max().getAsInt();
int freq[] = new int [max + 1 ];
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
freq[arr[i]]++;
}
for ( int i = 0 ; i < n; i++)
{
for ( int j = 2 * arr[i]; j <= max; j += arr[i])
{
if (freq[j] >= 1 )
{
count += freq[j];
}
}
if (freq[arr[i]] > 1 )
{
count += freq[arr[i]] - 1 ;
freq[arr[i]]--;
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 3 , 2 , 4 , 2 , 6 };
int n = arr.length;
System.out.println(freqPairs(arr, n));
}
}
|
Python3
def freqPairs(arr, n):
max = arr[ 0 ]
for i in range ( len (arr)):
if arr[i] > max :
max = arr[i]
freq = [ 0 for i in range ( max + 1 )]
count = 0
for i in range (n):
freq[arr[i]] + = 1
for i in range (n):
for j in range ( 2 * arr[i],
max + 1 , arr[i]):
if (freq[j] > = 1 ):
count + = freq[j]
if (freq[arr[i]] > 1 ):
count + = freq[arr[i]] - 1
freq[arr[i]] - = 1
return count
if __name__ = = '__main__' :
arr = [ 3 , 2 , 4 , 2 , 6 ]
n = len (arr)
print (freqPairs(arr, n))
|
C#
using System;
using System.Linq;
class GFG
{
static int freqPairs( int []arr, int n)
{
int max = arr.Max();
int []freq = new int [max + 1];
int count = 0;
for ( int i = 0; i < n; i++)
{
freq[arr[i]]++;
}
for ( int i = 0; i < n; i++)
{
for ( int j = 2 * arr[i]; j <= max; j += arr[i])
{
if (freq[j] >= 1)
{
count += freq[j];
}
}
if (freq[arr[i]] > 1)
{
count += freq[arr[i]] - 1;
freq[arr[i]]--;
}
}
return count;
}
public static void Main(String []args)
{
int []arr = {3, 2, 4, 2, 6};
int n = arr.Length;
Console.WriteLine(freqPairs(arr, n));
}
}
|
PHP
<?php
function freqPairs( $arr , $n )
{
$max = max( $arr );
$freq = array_fill (0, $max + 1, 0);
$count = 0;
for ( $i = 0; $i < $n ; $i ++)
$freq [ $arr [ $i ]]++;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 2 * $arr [ $i ];
$j <= $max ; $j += $arr [ $i ])
{
if ( $freq [ $j ] >= 1)
$count += $freq [ $j ];
}
if ( $freq [ $arr [ $i ]] > 1)
{
$count += $freq [ $arr [ $i ]] - 1;
$freq [ $arr [ $i ]]--;
}
}
return $count ;
}
$arr = array (3, 2, 4, 2, 6);
$n = count ( $arr );
echo freqPairs( $arr , $n );
?>
|
Javascript
<script>
function freqPairs(arr, n)
{
let max = Math.max(...arr);
let freq = new Array(max + 1).fill(0);
let count = 0;
for (let i = 0; i < n; i++)
freq[arr[i]]++;
for (let i = 0; i < n; i++) {
for (let j = 2 * arr[i]; j <= max; j += arr[i]) {
if (freq[j] >= 1)
count += freq[j];
}
if (freq[arr[i]] > 1) {
count += freq[arr[i]] - 1;
freq[arr[i]]--;
}
}
return count;
}
let arr = [ 3, 2, 4, 2, 6 ];
let n = arr.length;
document.write(freqPairs(arr, n));
</script>
|
Time Complexity: O(max*N), where max is the maximum value of the array.
Auxiliary Space: O(max)
Share your thoughts in the comments
Please Login to comment...