Find the GCD of LCM of all unique pairs in an Array
Last Updated :
10 Apr, 2023
Given an integer array arr[] of size N, the task is to find the GCD of LCM of all unique pair (i, j) of the array, such that i < j.
Examples:
Input: arr[] = {10, 24, 40, 80}
Output: 40
Explanation:
LCM of all unique pairs following given conditions are:
LCM(10, 24) = 120
LCM(10, 40) = 40
LCM(10, 80) = 80
LCM(24, 40) = 120
LCM(24, 80) = 240
LCM(40, 80) = 80
Therefore, GCD of these LCM = GCD(120, 40, 80, 120, 240, 80) = 40
Input: arr[] = {1, 1}
Output: 1
Naive Approach: The simplest approach is the find all unique pairs in the array. Then find their LCM. Then find the GCD of all the LCM.
Efficient Approach: The above naive approach can be optimised with the help of Suffix array. We can use the Suffix array to find the LCM of each element paired with other elements, efficiently. Then we can simply find and return the GCD of this LCM array.
- For every element A[i], we need to calculate LCM(a[i], a[j]), where j belong to [i+1, N-1].
- LCM of all pairs where starting element is A[i] can be written as
LCM(A[i], GCD(all j in range i+1 to n-1))
- For that we build a suffix array. Let say suffix[] that stores the gcd of elements that belong to the range [i+1, N-1].
- Then create a LCM array to store the LCM of A[i] and GCD of all elements after it, i.e.
LCM[i] = LCM(A[i], suffix[i+1])
Where suffix[i+1] stores the GCD of elements [i+1, n-1]
- Finally compute the GCD of all elements in LCM array.
C++
#include <bits/stdc++.h>
using namespace std;
int LCM( int x, int y)
{
return x * y / __gcd(x, y);
}
void gcd_of_lcm( int n, int arr[])
{
int suff[n];
for ( int x = 0; x < n; x++)
{
suff[x] = 1;
}
suff[n - 1] = arr[n - 1];
for ( int i = n - 2; i >= 0; i--)
{
suff[i] = __gcd(arr[i], suff[i + 1]);
}
vector< int > lcm;
for ( int i = 0; i < n - 1; i++)
{
int y = LCM(arr[i], suff[i + 1]);
lcm.push_back(y);
}
int ans = lcm[0];
for ( int i = 1; i < n - 1; i++)
{
ans = __gcd(ans, lcm[i]);
}
cout << ans << endl;
}
int main()
{
int n = 4;
int a[] = { 10, 24, 40, 80 };
gcd_of_lcm(n, a);
n = 10;
int b[] = { 540, 648, 810, 648, 720,
540, 594, 864, 972, 648 };
gcd_of_lcm(n, b);
}
|
Java
class GFG{
static int gcd( int x, int y)
{
if (y == 0 )
return x;
else
return gcd(y, x % y);
}
static void gcd_of_lcm( int n, int arr[])
{
int suff[] = new int [n];
for ( int i = 0 ; i < n; i++)
suff[i] = 1 ;
suff[n - 1 ] = arr[n - 1 ];
for ( int i = n - 2 ; i >= 0 ; i--)
suff[i] = gcd(arr[i], suff[i + 1 ]);
int lcm[] = new int [n - 1 ];
for ( int i = 0 ; i < n - 1 ; i++)
lcm[i] = (arr[i] * suff[i + 1 ]) /
gcd(arr[i], suff[i + 1 ]);
int ans = lcm[ 0 ];
for ( int i = 1 ; i < n - 1 ; i++)
{
ans = gcd(ans, lcm[i]);
}
System.out.println(ans);
}
public static void main(String[] args)
{
int n = 4 ;
int a[] = { 10 , 24 , 40 , 80 };
gcd_of_lcm(n, a);
n = 10 ;
int b[] = { 540 , 648 , 810 , 648 , 720 ,
540 , 594 , 864 , 972 , 648 };
gcd_of_lcm(n, b);
}
}
|
Python3
from math import gcd
def LCM(x, y):
return (x * y) / / gcd(x, y)
def gcd_of_lcm(n, arr):
suff = [ 1 ] * n
suff[n - 1 ] = arr[n - 1 ]
for i in range (n - 2 , - 1 , - 1 ):
suff[i] = gcd(arr[i], suff[i + 1 ])
lcm = []
for i in range (n - 1 ):
y = LCM( arr[i], suff[i + 1 ])
lcm.append(y)
ans = lcm[ 0 ]
for i in range ( 1 , n - 1 ):
ans = gcd(ans, lcm[i])
print (ans)
if __name__ = = "__main__" :
n = 4
a = [ 10 , 24 , 40 , 80 ]
gcd_of_lcm(n, a)
n = 10
a = [ 540 , 648 , 810 , 648 , 720 ,
540 , 594 , 864 , 972 , 648 ]
gcd_of_lcm(n, a)
|
C#
using System;
class GFG{
static int gcd( int x, int y)
{
if (y == 0)
return x;
else
return gcd(y, x % y);
}
static void gcd_of_lcm( int n, int [] arr)
{
int [] suff = new int [n];
for ( int i = 0; i < n; i++)
suff[i] = 1;
suff[n - 1] = arr[n - 1];
for ( int i = n - 2; i >= 0; i--)
suff[i] = gcd(arr[i], suff[i + 1]);
int [] lcm = new int [n - 1];
for ( int i = 0; i < n - 1; i++)
lcm[i] = (arr[i] * suff[i + 1]) /
gcd(arr[i], suff[i + 1]);
int ans = lcm[0];
for ( int i = 1; i < n - 1; i++)
{
ans = gcd(ans, lcm[i]);
}
Console.WriteLine(ans);
}
public static void Main()
{
int n = 4;
int [] a = { 10, 24, 40, 80 };
gcd_of_lcm(n, a);
n = 10;
int [] b = { 540, 648, 810, 648, 720,
540, 594, 864, 972, 648 };
gcd_of_lcm(n, b);
}
}
|
Javascript
<script>
function gcd(x, y)
{
if (y == 0)
return x;
else
return gcd(y, x % y);
}
function gcd_of_lcm(n, arr)
{
let suff = new Array(n);
for (let i = 0; i < n; i++)
suff[i] = 1;
suff[n - 1] = arr[n - 1];
for (let i = n - 2; i >= 0; i--)
suff[i] = gcd(arr[i], suff[i + 1]);
let lcm = new Array(n - 1);
for (let i = 0; i < n - 1; i++)
lcm[i] = parseInt((arr[i] * suff[i + 1]) /
gcd(arr[i], suff[i + 1]), 10);
let ans = lcm[0];
for (let i = 1; i < n - 1; i++)
{
ans = gcd(ans, lcm[i]);
}
document.write(ans + "</br>" );
}
let n = 4;
let a = [ 10, 24, 40, 80 ];
gcd_of_lcm(n, a);
n = 10;
let b = [ 540, 648, 810, 648, 720,
540, 594, 864, 972, 648 ];
gcd_of_lcm(n, b);
</script>
|
Time Complexity: O(N * log M), where M is the maximum element in the array.
Space Complexity: O(N)
Share your thoughts in the comments
Please Login to comment...