Count of pairs (i, j) in the array such that arr[i] is a factor of arr[j]

Given an array of integers arr, the task is to calculate the number of pairs (i, j) where i < j such that arr[j] % arr[i] = 0.

Examples:

Input: arr[] = {1, 2, 3, 4, 5}
Output: 5

Input: arr[] = {1, 1, 2, 2, 3, 3}
Output: 11

Approach 1:
Iterate over all pairs of the array and keep incrementing the count of pairs that satisfy the required condition.



Below code is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to find
// the number of pairs
// (i, j) such that arr[i]
// is a factor of arr[j]
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the
// count of Pairs
int numPairs(int arr[], int n)
{
    int ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (arr[j] % arr[i] == 0)
                ans++;
        }
    }
    return ans;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 1, 2, 2, 3, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << numPairs(arr, n) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to find the number of pairs
// (i, j) such that arr[i] is a factor of arr[j]
import java.util.*;
import java.lang.*;
class GFG{
  
// Function to return the
// count of Pairs
static int numPairs(int arr[], int n)
{
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++) 
        {
            if (arr[j] % arr[i] == 0)
                ans++;
        }
    }
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 1, 2, 2, 3, 3 };
    int n = arr.length;
  
    System.out.println(numPairs(arr, n));
}
}
  
// This code is contributed by offbeat

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the number
# of pairs (i, j) such that arr[i] 
# is a factor of arr[j] 
  
# Function to return the 
# count of Pairs 
def numPairs(arr, n): 
  
    ans = 0
    for i in range(n): 
        for j in range(i + 1, n): 
              
            if arr[j] % arr[i] == 0
                ans += 1
  
    return ans 
  
# Driver code 
arr = [ 1, 1, 2, 2, 3, 3
n = len(arr) 
  
print(numPairs(arr, n)) 
  
# This code is contributed by divyamohan123

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find the number of pairs
// (i, j) such that arr[i] is a factor of arr[j]
using System;
  
class GFG{
  
// Function to return the
// count of Pairs
static int numPairs(int []arr, int n)
{
    int ans = 0;
    for(int i = 0; i < n; i++)
    {
       for(int j = i + 1; j < n; j++) 
       {
          if (arr[j] % arr[i] == 0)
              ans++;
       }
    }
    return ans;
}
  
// Driver code
public static void Main()
{
    int []arr = { 1, 1, 2, 2, 3, 3 };
    int n = arr.Length;
  
    Console.Write(numPairs(arr, n));
}
}
  
// This code is contributed by Code_Mech

chevron_right


Output:

11

Approach 2: Store the indices of all array elements in a map. Traverse the map and for every occurrence of an element:

  • Add the occurrences of the same element after the current occurence.
  • Add the occurrences of all its multiples after the current occurrence using upper_bound

Below code is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to find
// the number of pairs
// (i, j) such that arr[i]
// is a factor of arr[j]
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the
// count of Pairs
int numPairs(int arr[], int n)
{
    map<int, vector<int> > mp;
    int mx = 0;
    for (int i = 0; i < n; i++) {
  
        // Update the maximum
        mx = max(mx, arr[i]);
  
        // Store the indices of
        // every element
        mp[arr[i]].push_back(i);
    }
  
    int ans = 0;
    for (auto i : mp) {
  
        int ctr = 1;
  
        // Access all indices of i
        for (int j : i.second) {
  
            // Add the number of
            // occurences of i
            // after j-th index
            ans += i.second.size() - ctr;
  
            // Traverse all multiples of i
            for (int k = 2 * i.first;
                 k <= mx;
                 k += i.first) {
  
                // Find their occurrences
                // after the j-th index
                int numGreater = 0;
                if (mp.find(k) != mp.end())
                    numGreater
                        = int(
                            mp[k]
                                .end()
                            - upper_bound(
                                  mp[k].begin(),
                                  mp[k].end(), j));
                // Add the count
                ans += numGreater;
            }
            ctr++;
        }
    }
  
    return ans;
}
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << numPairs(arr, n) << endl;
    return 0;
}

chevron_right


Output:

5

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.