Given an array a of size N . The task is to find the number of pairs such that gcd(a[i], a[j]) is equal to 1 , where 1 ? i < j ? N .
Examples:
Input : a[] = {1, 2, 4, 6} Output : 3 {1, 2}, {1, 4}, {1, 6} are such pairs
Input : a[] = {1, 2, 3, 4, 5, 6} Output : 11
Approach : The answer is to sum of ?(X) * C(D(X), 2) overall integer X . Where, ?(X) is Mobius function , C(N, K) is the selection of K things from N and D(X) is the number of integers in the given sequence that are divisible by X . The correctness of the solution follows from the fact that we can do an inclusion-exclusion principle solution and to show that it is, in fact, equal to our answer. That means that we will add to the answer the number of pairs that are divisible by some intermediate (in the IEP) product D if D is formed by multiplication of even number of prime numbers and subtract this number of pairs otherwise.
So, we get:
1 for addition, because that is Möbius function for square-free numbers with even number of prime divisors.
-1 for subtraction, that is Mobius function for square-free numbers with an odd number of prime divisors.
0 for square-free numbers. By the definition, they can’t occur in our IEP solution.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
#define N 100050
int lpf[N], mobius[N];
void least_prime_factor()
{
for (int i = 2; i < N; i++)
if (!lpf[i])
for (int j = i; j < N; j += i)
if (!lpf[j])
lpf[j] = i;
}
void Mobius()
{
for (int i = 1; i < N; i++) {
if (i == 1)
mobius[i] = 1;
else {
if (lpf[i / lpf[i]] == lpf[i])
mobius[i] = 0;
else
mobius[i] = -1 * mobius[i / lpf[i]];
}
}
}
int gcd_pairs(int a[], int n)
{
int maxi = 0;
int fre[N] = { 0 };
for (int i = 0; i < n; i++) {
fre[a[i]]++;
maxi = max(a[i], maxi);
}
least_prime_factor();
Mobius();
int ans = 0;
for (int i = 1; i <= maxi; i++) {
if (!mobius[i])
continue;
int temp = 0;
for (int j = i; j <= maxi; j += i)
temp += fre[j];
ans += temp * (temp - 1) / 2 * mobius[i];
}
return ans;
}
int main()
{
int a[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof(a) / sizeof(a[0]);
cout << gcd_pairs(a, n);
return 0;
}
Java
class GFG
{
static int N = 100050;
static int []lpf = new int[N];
static int []mobius = new int[N];
static void least_prime_factor()
{
for (int i = 2; i < N; i++)
if (lpf[i] == 0)
for (int j = i; j < N; j += i)
if (lpf[j] == 0)
lpf[j] = i;
}
static void Mobius()
{
for (int i = 1; i < N; i++)
{
if (i == 1)
mobius[i] = 1;
else
{
if (lpf[i / lpf[i]] == lpf[i])
mobius[i] = 0;
else
mobius[i] = -1 * mobius[i / lpf[i]];
}
}
}
static int gcd_pairs(int a[], int n)
{
int maxi = 0;
int []fre = new int[N];
for (int i = 0; i < n; i++)
{
fre[a[i]]++;
maxi = Math.max(a[i], maxi);
}
least_prime_factor();
Mobius();
int ans = 0;
for (int i = 1; i <= maxi; i++)
{
if (mobius[i] == 0)
continue;
int temp = 0;
for (int j = i; j <= maxi; j += i)
temp += fre[j];
ans += temp * (temp - 1) / 2 * mobius[i];
}
return ans;
}
public static void main (String[] args)
{
int a[] = { 1, 2, 3, 4, 5, 6 };
int n = a.length;
System.out.print(gcd_pairs(a, n));
}
}
Python3
N = 100050
lpf = [0 for i in range(N)]
mobius = [0 for i in range(N)]
def least_prime_factor():
for i in range(2, N):
if (lpf[i] == 0):
for j in range(i, N, i):
if (lpf[j] == 0):
lpf[j] = i
def Mobius():
for i in range(1, N):
if (i == 1):
mobius[i] = 1
else:
if (lpf[ (i // lpf[i]) ] == lpf[i]):
mobius[i] = 0
else:
mobius[i] = -1 * mobius[i // lpf[i]]
def gcd_pairs(a, n):
maxi = 0
fre = [0 for i in range(N)]
for i in range(n):
fre[a[i]] += 1
maxi = max(a[i], maxi)
least_prime_factor()
Mobius()
ans = 0
for i in range(1, maxi + 1):
if (mobius[i] == 0):
continue
temp = 0
for j in range(i, maxi + 1, i):
temp += fre[j]
ans += temp * (temp - 1) // 2 * mobius[i]
return ans
a = [1, 2, 3, 4, 5, 6]
n = len(a)
print(gcd_pairs(a, n))
C#
using System;
class GFG
{
static int N = 100050;
static int []lpf = new int[N];
static int []mobius = new int[N];
static void least_prime_factor()
{
for (int i = 2; i < N; i++)
if (lpf[i] == 0)
for (int j = i; j < N; j += i)
if (lpf[j] == 0)
lpf[j] = i;
}
static void Mobius()
{
for (int i = 1; i < N; i++)
{
if (i == 1)
mobius[i] = 1;
else
{
if (lpf[i / lpf[i]] == lpf[i])
mobius[i] = 0;
else
mobius[i] = -1 * mobius[i / lpf[i]];
}
}
}
static int gcd_pairs(int []a, int n)
{
int maxi = 0;
int []fre = new int[N];
for (int i = 0; i < n; i++)
{
fre[a[i]]++;
maxi = Math.Max(a[i], maxi);
}
least_prime_factor();
Mobius();
int ans = 0;
for (int i = 1; i <= maxi; i++)
{
if (mobius[i] == 0)
continue;
int temp = 0;
for (int j = i; j <= maxi; j += i)
temp += fre[j];
ans += temp * (temp - 1) / 2 * mobius[i];
}
return ans;
}
public static void Main (String[] args)
{
int []a = { 1, 2, 3, 4, 5, 6 };
int n = a.Length;
Console.Write(gcd_pairs(a, n));
}
}
Javascript
<script>
var N = 100050;
var lpf = Array(N).fill(0);
var mobius = Array(N).fill(0);
function least_prime_factor() {
for (i = 2; i < N; i++)
if (lpf[i] == 0)
for (j = i; j < N; j += i)
if (lpf[j] == 0)
lpf[j] = i;
}
function Mobius() {
for (i = 1; i < N; i++) {
if (i == 1)
mobius[i] = 1;
else {
if (lpf[i / lpf[i]] == lpf[i])
mobius[i] = 0;
else
mobius[i] = -1 * mobius[i / lpf[i]];
}
}
}
function gcd_pairs(a , n) {
var maxi = 0;
var fre = Array(n+1).fill(0);
for (i = 0; i < n; i++) {
fre[a[i]]++;
maxi = Math.max(a[i], maxi);
}
least_prime_factor();
Mobius();
var ans = 0;
for (i = 1; i <= maxi; i++) {
if (mobius[i] == 0)
continue;
var temp = 0;
for (j = i; j <= maxi; j += i)
temp = parseInt(temp+fre[j]);
ans += parseInt(temp * (temp - 1) / 2 * mobius[i]);
}
return ans;
}
var a = [ 1, 2, 3, 4, 5, 6 ];
var n = a.length;
document.write(gcd_pairs(a, n));
</script>
Time Complexity: O(N2 )
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule. Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
18 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...