Java程序通过GCD操作使所有数组元素等于1
给定一个由 N 个整数组成的数组 A[]。您的任务是使最终数组的所有元素都等于 1。您可以多次执行以下操作(可能为零)。选择两个索引 , (0<=i,j 例子 : Input : A[] = {2 , 4 , 6 ,9} Output: Yes Explanation: First choose 4 and 9 their GCD will be 1, so now the array is {2, 1, 6, 1}. Now, we can choose 2 and 1, their GCD is 1 so, the array becomes {1,1,6,1}. Finally we can choose any 1 with 6 as their GCD is 1. Thus the final array becomes {1, 1, 1, 1}. So, we can make all the array elements equal to 1. Input : A[] = {9 , 6 , 15} Output: No 天真的方法: 下面是上述方法的实现: 时间复杂度: O(N^2*log N),其中 N 是数组的长度。 有效的方法: 下面是上述方法的实现: 时间复杂度: O(N*logM),其中 N 是数组的长度,M 是数组的最小元素。 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
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