Check if all array elements are pairwise co-prime or not
Last Updated :
29 Jan, 2024
Given an array A[] consisting of N positive integers, the task is to check if all the array elements are pairwise co-prime, i.e. for all pairs (Ai , Aj), such that 1<=i<j<=N, GCD(Ai, Aj) = 1.
Examples:
Input : A[] = {2, 3, 5}
Output : Yes
Explanation : All the pairs, (2, 3), (3, 5), (2, 5) are pairwise co-prime.
Input : A[] = {5, 10}
Output : No
Explanation : GCD(5, 10)=5 so they are not co-prime.
Naive Approach: The simplest approach to solve the problem is to generate all possible pairs from a given array and for each pair, check if it is coprime or not. If any pair is found to be non-coprime, print “No“. Otherwise, print “Yes“.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the following observation:
If any two numbers have a common prime factor, then their GCD can never be 1.
This can also be interpreted as:
The LCM of the array must be equal to the product of the elements in the array.
Therefore, the solution boils down to calculating the LCM of the given array and check if it is equal to the product of all the array elements or not.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll GCD(ll a, ll b)
{
if (a == 0)
return b;
return GCD(b % a, a);
}
ll LCM(ll a, ll b)
{
return (a * b)
/ GCD(a, b);
}
void checkPairwiseCoPrime( int A[], int n)
{
ll prod = 1;
ll lcm = 1;
for ( int i = 0; i < n; i++) {
prod *= A[i];
lcm = LCM(A[i], lcm);
}
if (prod == lcm)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
int main()
{
int A[] = { 2, 3, 5 };
int n = sizeof (A) / sizeof (A[0]);
checkPairwiseCoPrime(A, n);
}
|
Java
import java.util.*;
import java.lang.*;
class GFG{
static long GCD( long a, long b)
{
if (a == 0 )
return b;
return GCD(b % a, a);
}
static long LCM( long a, long b)
{
return (a * b) / GCD(a, b);
}
static void checkPairwiseCoPrime( int A[], int n)
{
long prod = 1 ;
long lcm = 1 ;
for ( int i = 0 ; i < n; i++)
{
prod *= A[i];
lcm = LCM(A[i], lcm);
}
if (prod == lcm)
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main (String[] args)
{
int A[] = { 2 , 3 , 5 };
int n = A.length;
checkPairwiseCoPrime(A, n);
}
}
|
Python3
def GCD(a, b):
if (a = = 0 ):
return b
return GCD(b % a, a)
def LCM(a, b):
return (a * b) / / GCD(a, b)
def checkPairwiseCoPrime(A, n):
prod = 1
lcm = 1
for i in range (n):
prod * = A[i]
lcm = LCM(A[i], lcm)
if (prod = = lcm):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
A = [ 2 , 3 , 5 ]
n = len (A)
checkPairwiseCoPrime(A, n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static long GCD( long a,
long b)
{
if (a == 0)
return b;
return GCD(b % a, a);
}
static long LCM( long a,
long b)
{
return (a * b) / GCD(a, b);
}
static void checkPairwiseCoPrime( int []A,
int n)
{
long prod = 1;
long lcm = 1;
for ( int i = 0; i < n; i++)
{
prod *= A[i];
lcm = LCM(A[i], lcm);
}
if (prod == lcm)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
public static void Main(String[] args)
{
int []A = {2, 3, 5};
int n = A.Length;
checkPairwiseCoPrime(A, n);
}
}
|
Javascript
<script>
function GCD(a , b) {
if (a == 0)
return b;
return GCD(b % a, a);
}
function LCM(a , b) {
return (a * b) / GCD(a, b);
}
function checkPairwiseCoPrime(A , n) {
var prod = 1;
var lcm = 1;
for (i = 0; i < n; i++) {
prod *= A[i];
lcm = LCM(A[i], lcm);
}
if (prod == lcm)
document.write( "Yes" );
else
document.write( "No" );
}
var A = [ 2, 3, 5 ];
var n = A.length;
checkPairwiseCoPrime(A, n);
</script>
|
Time Complexity: O(N log (min(A[i])))
Auxiliary Space: O(1)
Approach#2:using nested loop
Algorithm
1. Define a function named are_elements_pairwise_coprime that takes an array arr as input.
2.Initialize a variable n to the length of the array arr.
3.Loop over all pairs of elements in the array using nested loops, i.e., for each element arr[i], loop over all elements arr[j] where j > i.
4.For each pair of elements, compute their GCD using the gcd function defined below.
5.If the GCD of any pair of elements is not equal to 1, return “No”.
6.If all pairs of elements have a GCD equal to 1, return “Yes”.
C++
#include <iostream>
using namespace std;
int gcd( int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
string areElementsPairwiseCoprime( int arr[], int n) {
for ( int i = 0; i < n; ++i) {
for ( int j = i + 1; j < n; ++j) {
if (gcd(arr[i], arr[j]) != 1) {
return "No" ;
}
}
}
return "Yes" ;
}
int main() {
int arr[] = {2, 3, 5};
int n = sizeof (arr) / sizeof (arr[0]);
cout << areElementsPairwiseCoprime(arr, n) << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
static int gcd( int a, int b) {
if (b == 0 ) {
return a;
} else {
return gcd(b, a % b);
}
}
static String areElementsPairwiseCoprime( int [] arr) {
int n = arr.length;
for ( int i = 0 ; i < n; ++i) {
for ( int j = i + 1 ; j < n; ++j) {
if (gcd(arr[i], arr[j]) != 1 ) {
return "No" ;
}
}
}
return "Yes" ;
}
public static void main(String[] args) {
int [] arr = { 2 , 3 , 5 };
System.out.println(areElementsPairwiseCoprime(arr));
}
}
|
Python3
def are_elements_pairwise_coprime(arr):
n = len (arr)
for i in range (n):
for j in range (i + 1 , n):
if gcd(arr[i], arr[j]) ! = 1 :
return "No"
return "Yes"
def gcd(a, b):
if b = = 0 :
return a
else :
return gcd(b, a % b)
arr = [ 2 , 3 , 5 ]
print (are_elements_pairwise_coprime(arr))
|
C#
using System;
class Program {
static int GCD( int a, int b)
{
if (b == 0) {
return a;
}
else {
return GCD(b, a % b);
}
}
static string AreElementsPairwiseCoprime( int [] arr,
int n)
{
for ( int i = 0; i < n; ++i) {
for ( int j = i + 1; j < n; ++j) {
if (GCD(arr[i], arr[j]) != 1) {
return "No" ;
}
}
}
return "Yes" ;
}
static void Main()
{
int [] arr = { 2, 3, 5 };
int n = arr.Length;
Console.WriteLine(
AreElementsPairwiseCoprime(arr, n));
}
}
|
Javascript
function gcd(a, b) {
if (b === 0) {
return a;
} else {
return gcd(b, a % b);
}
}
function areElementsPairwiseCoprime(arr) {
for (let i = 0; i < arr.length; ++i) {
for (let j = i + 1; j < arr.length; ++j) {
if (gcd(arr[i], arr[j]) !== 1) {
return "No" ;
}
}
}
return "Yes" ;
}
const arr = [2, 3, 5];
console.log(areElementsPairwiseCoprime(arr));
|
The time complexity of this algorithm is O(n^2), where n is the length of the input array, since we are checking all possible pairs of elements.
The space complexity is O(1), since we are not using any additional data structures.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...