Open In App

Count number of pairs not divisible by any element in the array

Last Updated : 24 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] of size N, the task is to count the number of pairs of integers (i, j) for which there does not exist an integer k such that arr[i] is divisible by arr[k] and arr[j] is divisible by arr[k], such that k can be any index between [0, N – 1].

Examples:

Input: N = 4, arr[] = {2, 4, 5, 7}
Output: 5
Explanation: The 5 pairs are:

  • (0, 2): arr[0] = 2, arr[2] = 5 and no number in arr[] divides both 2 and 5.
  • (0, 3): arr[0] = 2, arr[3] = 7 and no number in arr[] divides both 2 and 7.
  • (1, 2): arr[1] = 4, arr[2] = 5 and no number in arr[] divides both 4 and 5.
  • (1, 3): arr[1] = 4, arr[3] = 7 and no number in arr[] divides both 4 and 7.
  • (2, 3): arr[2] = 5, arr[3] = 7 and no number in arr[] divides both 5 and 7.

Input: N = 3, arr[] = {10, 15, 5}
Output: 0
Explanation: All pairs are divisible by some number in the array arr[].

Approach: To solve the problem, follow the below idea:

The naive approach will be to iterate over all the pairs and then calculate gcd for every pair. After calculating the gcd, again iterate over the entire array to check if the pair is divisible by any integer in arr[]. The time complexity of this approach would be O(N * N * N). We can use a different approach to reduce the runtime complexity.

Instead of iterating over all the pairs and then checking for the valid pairs, we can find the total number of pairs that can be formed, that is (N * (N – 1))/2 and then subtract the invalid pairs. Start from one element and find all its multiples in the array. Lets say, there are X multiples of the element, so now each of these multiples can pair with one another to give (X * (X – 1))/2 pairs. These pairs will not contribute to the answer and hence will be subtracted from the total number of pairs. Similarly, we can subtract all the invalid pairs to get the final answer.

Step-by-step algorithm:

  • Sort the array so that we only need to search the elements present after the current index for the multiples.
  • Initialize the total pairs = (N * (N + 1))/2
  • Iterate over the array and for every index i, count all the multiples of arr[i] in the whole array, say cnt.
  • Subtract (cnt * (cnt + 1)) / 2 from the total number of pairs as these pairs won’t contribute to the answer.
  • Return the final answer after subtracting all the invalid pairs.

Below is the implementation of the approach:

C++




#include <bits/stdc++.h>
using namespace std;
 
int solve(int *arr, int N) {
    // sort the array
    sort(arr, arr + N);
 
    // Initialize the answer with the total number of pairs possible
    int ans = (N * (N - 1)) / 2;
    for(int i = 0; i < N; i++) {
        int cnt = 0;
        for(int j = i; j < N; j++) {
            if(arr[j] % arr[i] == 0) {
                cnt += 1;
            }
        }
        ans -= (cnt * (cnt - 1))/2;
    }
return ans;
}
 
int main() {
    // Sample Input
    int N = 4;
    int arr[] = {2, 4, 5, 7};
     
    cout << solve(arr, N) << endl;
    return 0;
}


Java




import java.util.Arrays;
 
public class Main {
    static int solve(int[] arr, int N) {
        // Sort the array
        Arrays.sort(arr);
 
        // Initialize the answer with the total number of pairs possible
        int ans = (N * (N - 1)) / 2;
 
        for (int i = 0; i < N; i++) {
            int cnt = 0;
            for (int j = i; j < N; j++) {
                if (arr[j] % arr[i] == 0) {
                    cnt += 1;
                }
            }
            ans -= (cnt * (cnt - 1)) / 2;
        }
        return ans;
    }
 
    public static void main(String[] args) {
        // Sample Input
        int N = 4;
        int[] arr = {2, 4, 5, 7};
 
        System.out.println(solve(arr, N));
    }
}
 
// This code is contributed by shivamgupta0987654321


Python3




def solve(arr, N):
    # Sort the array
    arr.sort()
 
    # Initialize the answer with the total number of pairs possible
    ans = (N * (N - 1)) // 2
 
    for i in range(N):
        cnt = 0
        for j in range(i, N):
            if arr[j] % arr[i] == 0:
                cnt += 1
        ans -= (cnt * (cnt - 1)) // 2
 
    return ans
 
# Sample Input
N = 4
arr = [2, 4, 5, 7]
 
# Function call
print(solve(arr, N))


C#




using System;
 
public class Program
{
    static int Solve(int[] arr, int N)
    {
        // Sort the array
        Array.Sort(arr);
 
        // Initialize the answer with the total number of pairs possible
        int ans = (N * (N - 1)) / 2;
 
        for (int i = 0; i < N; i++)
        {
            int cnt = 0;
            for (int j = i; j < N; j++)
            {
                if (arr[j] % arr[i] == 0)
                {
                    cnt += 1;
                }
            }
            ans -= (cnt * (cnt - 1)) / 2;
        }
        return ans;
    }
 
    public static void Main(string[] args)
    {
        // Sample Input
        int N = 4;
        int[] arr = { 2, 4, 5, 7 };
 
        Console.WriteLine(Solve(arr, N));
    }
}


Javascript




function solve(arr) {
    // Get the length of the array
    let N = arr.length;
 
    // Sort the array
    arr.sort((a, b) => a - b);
 
    // Initialize the answer with the total number of pairs possible
    let ans = (N * (N - 1)) / 2;
    for(let i = 0; i < N; i++) {
        let cnt = 0;
        for(let j = i; j < N; j++) {
            if(arr[j] % arr[i] === 0) {
                cnt += 1;
            }
        }
        ans -= (cnt * (cnt - 1)) / 2;
    }
    return ans;
}
 
// Sample Input
let N = 4;
let arr = [2, 4, 5, 7];
 
console.log(solve(arr));


Output

5


Time Complexity: O(N *N), where N is the size of array arr[].
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads