📜  检查所有数组元素是否成对互质

📅  最后修改于: 2021-10-25 10:32:52             🧑  作者: Mango

给定一个由N 个正整数组成的数组A[] ,任务是检查所有数组元素是否都是成对互质的,即对于所有对 (A i , A j ),使得 1<=iGCD(A i , A j ) = 1 。

例子:

朴素的方法:解决问题最简单的方法是从给定的数组中生成所有可能的对,对于每一对,检查它是否是互质的。如果发现任何对是非互质的,则打印“”。否则,打印“”。
时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:上述方法可以基于以下观察进行优化:

这也可以解释为:

因此,该解决方案归结为计算给定数组的 LCM 并检查它是否等于所有数组元素的乘积。

下面是上述方法的实现:

C++
// C++ Program for the above approach
#include 
using namespace std;
#define ll long long int
 
// Function to calculate GCD
ll GCD(ll a, ll b)
{
    if (a == 0)
        return b;
    return GCD(b % a, a);
}
 
// Function to calculate LCM
ll LCM(ll a, ll b)
{
    return (a * b)
        / GCD(a, b);
}
 
// Function to check if all elements
// in the array are pairwise coprime
void checkPairwiseCoPrime(int A[], int n)
{
    // Initialize variables
    ll prod = 1;
    ll lcm = 1;
 
    // Iterate over the array
    for (int i = 0; i < n; i++) {
 
        // Calculate product of
        // array elements
        prod *= A[i];
 
        // Calculate LCM of
        // array elements
        lcm = LCM(A[i], lcm);
    }
 
    // If the product of array elements
    // is equal to LCM of the array
    if (prod == lcm)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}
// Driver Code
int main()
{
    int A[] = { 2, 3, 5 };
    int n = sizeof(A) / sizeof(A[0]);
 
    // Function call
    checkPairwiseCoPrime(A, n);
}


Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
 
class GFG{
 
// Function to calculate GCD
static long GCD(long a, long b)
{
    if (a == 0)
        return b;
         
    return GCD(b % a, a);
}
 
// Function to calculate LCM
static long LCM(long a, long b)
{
    return (a * b) / GCD(a, b);
}
 
// Function to check if all elements
// in the array are pairwise coprime
static void checkPairwiseCoPrime(int A[], int n)
{
     
    // Initialize variables
    long prod = 1;
    long lcm = 1;
 
    // Iterate over the array
    for(int i = 0; i < n; i++)
    {
         
        // Calculate product of
        // array elements
        prod *= A[i];
 
        // Calculate LCM of
        // array elements
        lcm = LCM(A[i], lcm);
    }
     
    // If the product of array elements
    // is equal to LCM of the array
    if (prod == lcm)
        System.out.println("Yes");
    else
        System.out.println("No");
}
 
// Driver Code
public static void main (String[] args)
{
    int A[] = { 2, 3, 5 };
    int n = A.length;
     
    // Function call
    checkPairwiseCoPrime(A, n);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
 
# Function to calculate GCD
def GCD(a, b):
     
    if (a == 0):
        return b
         
    return GCD(b % a, a)
 
# Function to calculate LCM
def LCM(a, b):
     
    return (a * b) // GCD(a, b)
 
# Function to check if aelements
# in the array are pairwise coprime
def checkPairwiseCoPrime(A, n):
     
    # Initialize variables
    prod = 1
    lcm = 1
 
    # Iterate over the array
    for i in range(n):
 
        # Calculate product of
        # array elements
        prod *= A[i]
 
        # Calculate LCM of
        # array elements
        lcm = LCM(A[i], lcm)
 
    # If the product of array elements
    # is equal to LCM of the array
    if (prod == lcm):
        print("Yes")
    else:
        print("No")
 
# Driver Code
if __name__ == '__main__':
     
    A = [ 2, 3, 5 ]
    n = len(A)
 
    # Function call
    checkPairwiseCoPrime(A, n)
 
# This code is contributed by mohit kumar 29


C#
// C# program for
// the above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Function to calculate GCD
static long GCD(long a,
                long b)
{
  if (a == 0)
    return b;
  return GCD(b % a, a);
}
 
// Function to calculate LCM
static long LCM(long a,
                long b)
{
  return (a * b) / GCD(a, b);
}
 
// Function to check if all elements
// in the array are pairwise coprime
static void checkPairwiseCoPrime(int []A,
                                 int n)
{    
  // Initialize variables
  long prod = 1;
  long lcm = 1;
 
  // Iterate over the array
  for(int i = 0; i < n; i++)
  {
    // Calculate product of
    // array elements
    prod *= A[i];
 
    // Calculate LCM of
    // array elements
    lcm = LCM(A[i], lcm);
  }
 
  // If the product of array elements
  // is equal to LCM of the array
  if (prod == lcm)
    Console.WriteLine("Yes");
  else
    Console.WriteLine("No");
}
 
// Driver Code
public static void Main(String[] args)
{
  int []A = {2, 3, 5};
  int n = A.Length;
 
  // Function call
  checkPairwiseCoPrime(A, n);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
Yes

时间复杂度: O(N log (min(A[i])))
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程