Java Program to Make All the Array Elements Equal to One by GCD Operations
Last Updated :
28 Jan, 2021
You are given an array A[] of N integers. Your task is to make all the elements of the final array equal to 1. You can perform the below operation any number of times (possibly zero). Choose two indices <i, j>, (0<=i,j<N) and replace Ai and Aj both with the GCD ( Greatest Common Divisor ) of Ai and Aj.
Example :
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
Naive Approach:
- If we get the GCD of any pair equal to 1, then we can make all array elements 1 by taking that number and 1 one by one.
- So, find any co-prime pair exists or not because the GCD of Co-prime pair is equal to 1.
- If the value of GCD pair is equal to 1, then print “Yes”.
- Else print “No”.
Below is the implementation of the above approach :
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");
}
}
Time Complexity: O(N^2*log N), where N is the length of an array.
Efficient Approach:
- Calculate the GCD of the whole array.
- If there exists any co-prime pair then their GCD will be 1.
- So after this, any number comes, the GCD will remain 1.
- If at any point in time the GCD becomes 1, then break the loop and print “Yes”.
- If the final value of GCD after the whole iteration is not equal to one, then print “No”.
Below is the implementation of the above approach :
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");
}
}
Time Complexity: O(N*logM), where N is the length of an array and M is the smallest element of an array.
Similar Reads
Java Program for GCD of more than two (or array) numbers The GCD of three or more numbers equals the product of the prime factors common to all the numbers, but it can also be calculated by repeatedly taking the GCDs of pairs of numbers. gcd(a, b, c) = gcd(a, gcd(b, c)) = gcd(gcd(a, b), c) = gcd(gcd(a, c), b) Java // Java program to find GCD of two or //
1 min read
Java Program to Find GCD and LCM of Two Numbers Using Euclidâs Algorithm GCD or the Greatest Common Divisor of two given numbers A and B is the highest number dividing both A and B completely, i.e., leaving remainder 0 in each case. LCM or the Least Common Multiple of two given numbers A and B is the Least number which can be divided by both A and B, leaving remainder 0
3 min read
Java Program to Find the GCDs of given index ranges in an array Write a Java program for a given array a[0 . . . n-1], the task is to find the GCD from index qs (query start) to qe (query end) where 0 <= qs <= qe <= n-1. Example: Input: arr[] = {2, 3, 60, 90, 50};Index Ranges: {1, 3}, {2, 4}, {0, 2}Output: GCDs of given ranges are 3, 10, 1Explanation: E
6 min read
Java Program to Find GCD or HCF of Two Numbers GCD (i.e. Greatest Common Divisor) or HCF (i.e. Highest Common Factor) is the largest number that can divide both the given numbers. Example: HCF of 10 and 20 is 10, and HCF of 9 and 21 is 3.Therefore, firstly find all the prime factors of both the stated numbers, then find the intersection of all t
2 min read
Java Program to Compute GCD GCD (Greatest Common Divisor) of two given numbers A and B is the highest number that can divide both A and B completely, i.e., leaving the remainder 0 in each case. GCD is also called HCF(Highest Common Factor). There are various approaches to finding the GCD of two given numbers.Approaches: The GC
5 min read
Minimize count of divisions by 2 required to make all array elements equal Given an array arr[] consisting of N positive integers, the task is to find the minimum count of divisions(integer division) of array elements by 2 to make all array elements the same. Examples: Input: arr[] = {3, 1, 1, 3}Output: 2Explanation:Operation 1: Divide arr[0] ( = 3) by 2. The array arr[] m
8 min read