Open In App

Java Program to Make All the Array Elements Equal to One by GCD Operations

Last Updated : 28 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
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: 

  1. 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.
  2. So, find any co-prime pair exists or not because the GCD of Co-prime pair is equal to 1.
  3. If the value of GCD pair is equal to 1, then print “Yes”.
  4. 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");
    }
}


Output

Yes

Time Complexity: O(N^2*log N), where N is the length of an array.

Efficient Approach:  

  1. Calculate the GCD of the whole array.
  2. If there exists any co-prime pair then their GCD will be 1.
  3. So after this, any number comes, the GCD will remain 1.
  4. If at any point in time the GCD becomes 1, then break the loop and print “Yes”.
  5. 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");
    }
}


Output

Yes

Time Complexity: O(N*logM), where N is the length of an array and M is the smallest element of an array.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads