📌  相关文章
📜  Java程序通过GCD操作使所有数组元素等于1

📅  最后修改于: 2022-05-13 01:54:42.498000             🧑  作者: Mango

Java程序通过GCD操作使所有数组元素等于1

给定一个由 N 个整数组成的数组 A[]。您的任务是使最终数组的所有元素都等于 1。您可以多次执行以下操作(可能为零)。选择两个索引 , (0<=i,j

例子 :

天真的方法:

  1. 如果我们得到任何一对的 GCD 等于 1,那么我们可以通过将这个数字和 1 一一取来使所有数组元素为 1。
  2. 因此,找出任何互质对是否存在,因为互质对的 GCD 等于 1。
  3. 如果 GCD pair 的值等于 1,则打印“Yes”。
  4. 否则打印“否”。

下面是上述方法的实现:

Java
// Java program to make all the array elements
// equal to one by GCD operations
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
  
    // Function that returns whether it is possible or not
    // to make all the array elements equal to one (1)
    static boolean possible(int A[])
    {
        int n = A.length;
        // Check all the possible pairs
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                int gcd = gcd(A[i], A[j]);
                // If the gcd is equal to 1 , return true
                if (gcd == 1)
                    return true;
            }
        }
        return false;
    }
    // Recursive function to return gcd of a and b
    static int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
    // Driver Code
    public static void main(String[] args)
    {
        // Given Array
        int A[] = { 2, 4, 6, 9 };
  
        boolean answer = possible(A);
  
        System.out.println(answer == true ? "Yes" : "No");
    }
}


Java
// Java program to make all the array elements
// equal to one by GCD operations
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
  
    // Function that returns whether it is possible or not
    // to make all the array elements equal to one (1)
    static boolean possible(int A[])
    {
        int n = A.length;
  
        int gcd = 0;
        // calculate GCD of the whole array
        for (int i = 0; i < n; i++) {
            gcd = gcd(gcd, A[i]);
            // If the gcd is equal to 1 , return true
            if (gcd == 1)
                return true;
        }
        return false;
    }
    // Recursive function to return gcd of a and b
    static int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
    // Driver Code
    public static void main(String[] args)
    {
        // Given Array
        int A[] = { 2, 4, 6, 9 };
  
        boolean answer = possible(A);
  
        System.out.println(answer == true ? "Yes" : "No");
    }
}


输出
Yes

时间复杂度: O(N^2*log N),其中 N 是数组的长度。

有效的方法:

  1. 计算整个数组的 GCD。
  2. 如果存在任何互质对,则它们的 GCD 将为 1。
  3. 所以在此之后,任何数字出现,GCD 将保持为 1。
  4. 如果在任何时间点 GCD 变为 1,则中断循环并打印“Yes”。
  5. 如果整个迭代后 GCD 的最终值不等于 1,则打印“否”。

下面是上述方法的实现:

Java

// Java program to make all the array elements
// equal to one by GCD operations
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
  
    // Function that returns whether it is possible or not
    // to make all the array elements equal to one (1)
    static boolean possible(int A[])
    {
        int n = A.length;
  
        int gcd = 0;
        // calculate GCD of the whole array
        for (int i = 0; i < n; i++) {
            gcd = gcd(gcd, A[i]);
            // If the gcd is equal to 1 , return true
            if (gcd == 1)
                return true;
        }
        return false;
    }
    // Recursive function to return gcd of a and b
    static int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
    // Driver Code
    public static void main(String[] args)
    {
        // Given Array
        int A[] = { 2, 4, 6, 9 };
  
        boolean answer = possible(A);
  
        System.out.println(answer == true ? "Yes" : "No");
    }
}
输出
Yes

时间复杂度: O(N*logM),其中 N 是数组的长度,M 是数组的最小元素。