Java Program to Make All the Array Elements Equal to One by GCD Operations Last Updated : 28 Jan, 2021 Comments Improve Suggest changes Like Article Like Report 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"); } } OutputYesTime 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"); } } OutputYesTime Complexity: O(N*logM), where N is the length of an array and M is the smallest element of an array. Comment More infoAdvertise with us Next Article Java Program to Make All the Array Elements Equal to One by GCD Operations jyoti369 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java 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 Minimum operations to make GCD of array a multiple of k Given an array and k, we need to find the minimum operations needed to make GCD of the array equal or multiple of k. Here an operation means either increment or decrements an array element by 1. Examples: Input : a = { 4, 5, 6 }, k = 5 Output : 2 Explanation : We can increase 4 by 1 so that it becom 8 min read Queries to calculate GCD of an array after multiplying first or last K elements by X Given an array arr[] consisting of N positive integers and a 2D array queries[][] of the type {a, K, X} such that if the value of a is 1, then multiply first K array elements by X. Otherwise, multiply last K array elements by X. The task is to calculate GCD of the array after performing each query o 10 min read Check if an Array can be converted to other by replacing pairs with GCD Given arrays A[] and B[] each of length N and A[i] has all the elements from 1 to N, the task is to check if it is possible to convert A[] to B[] by replacing any pair of A[] with their GCD. Examples: Input: N = 2, A[] = {1, 2}, B[] = {1, 1}Output: YES Explanation:First Operation - For A[], choose i 15 min read Maximum number which can divide all array element after one replacement Given an array arr, replace any one element in array with any other integer. The task is to return the maximum number which divides all elements in this array completely. Examples: Input: arr = [15, 9, 3]Output: 3Explanation: Here replace 15 with 3 so array becomes arr = [3, 9, 3] and now all elemen 7 min read Like