Open In App

Find if it is possible to make all elements of an array equal by the given operations

Last Updated : 28 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[], the task is to make all the array elements equal with the given operation. 
In a single operation, any element of the array can be either multiplied by 3 or by 5 any number of times. If it’s possible to make all the array elements equal with the given operation then print Yes else print No.
Examples: 
 

Input: arr[] = {18, 30, 54, 90, 162} 
Output: Yes 
Explanation: 
We can perform following operations: 
162 X 5 = 810 
90 X 3 X 3 = 810 
54 X 5 X 3 = 810 
30 X 3 X 3 X 3 = 810 
18 X 5 X 3 X 3 = 810
Input: arr[] = {18, 36, 58, 90, 162} 
Output: No 
Explanation: 
There is no way you can make all elements equal. 
 

 

Observations
 

  • If after some operations, all the numbers become equal then they will have the same Prime factorization i.e each number will have the same power of 2, 3, 5…and so on.
  • Since we are multiplying the numbers only by 3 and 5 which are Prime Numbers so we can make powers of 3 and 5 in the prime factorization of all numbers equal after some operations.
  • Therefore, for all numbers to be made equal, the powers of Prime Numbers in the prime factorization other than 3 and 5 must be equal.
  • The solution would be to take each number and remove all powers of 3 and 5 from it. If then all numbers turn out to be equal then it’s possible to make array elements equal by using the given operations otherwise it’s not possible.

Steps
 

  1. Divide each element of array arr[] with 3 and 5 such that all the power of 3 and 5 in Prime Factorization of each element becomes zero.
  2. Check if all the element of the array are equal or not. If yes then print Yes.
  3. Else print No.

Below is the implementation of the above approach: 
 

CPP




// C++ implementation to find if it's
// possible to make all elements of an
// array equal by using two operations.
#include <bits/stdc++.h>
using namespace std;
 
// Function to find if it's possible
// to make all array elements equal
bool canMakeEqual(int a[], int n)
{
    // Iterate over all numbers
    for (int i = 0; i < n; i++) {
 
        // If a number has a power of 5
        // remove it
        while (a[i] % 5 == 0) {
            a[i] /= 5;
        }
 
        // If a number has a power of 3
        // remove it
        while (a[i] % 3 == 0) {
            a[i] /= 3;
        }
    }
 
    int last = a[0];
 
    // Check if all elements are equal
    // in the final array
    for (int i = 1; i < n; i++) {
        if (a[i] != last) {
            return false;
        }
    }
 
    return true;
}
 
// Driver's Code
int main()
{
    int arr[] = { 18, 30, 54, 90, 162 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call to check if all
    // element in the array can be equal
    // or not.
    if (canMakeEqual(arr, n)) {
        cout << "YES" << endl;
    }
    else {
        cout << "NO" << endl;
    }
 
    return 0;
}


Java




// Java implementation to find if it's
// possible to make all elements of an
// array equal by using two operations.
class GFG{
  
// Function to find if it's possible
// to make all array elements equal
static boolean canMakeEqual(int a[], int n)
{
    // Iterate over all numbers
    for (int i = 0; i < n; i++) {
  
        // If a number has a power of 5
        // remove it
        while (a[i] % 5 == 0) {
            a[i] /= 5;
        }
  
        // If a number has a power of 3
        // remove it
        while (a[i] % 3 == 0) {
            a[i] /= 3;
        }
    }
  
    int last = a[0];
  
    // Check if all elements are equal
    // in the final array
    for (int i = 1; i < n; i++) {
        if (a[i] != last) {
            return false;
        }
    }
  
    return true;
}
  
// Driver's Code
public static void main(String[] args)
{
    int arr[] = { 18, 30, 54, 90, 162 };
  
    int n = arr.length;
  
    // Function call to check if all
    // element in the array can be equal
    // or not.
    if (canMakeEqual(arr, n)) {
        System.out.print("YES" +"\n");
    }
    else {
        System.out.print("NO" +"\n");
    }
}
}
 
// This code is contributed by PrinciRaj1992


Python3




# Python3 implementation to find if it's
# possible to make all elements of an
# array equal by using two operations.
 
# Function to find if it's possible
# to make all array elements equal
def canMakeEqual( a, n) :
 
    # Iterate over all numbers
    for i in range(n) :
 
        # If a number has a power of 5
        # remove it
        while (a[i] % 5 == 0) :
            a[i] //= 5;
         
        # If a number has a power of 3
        # remove it
        while (a[i] % 3 == 0) :
            a[i] //= 3;
 
    last = a[0];
 
    # Check if all elements are equal
    # in the final array
    for i in range(1,n) :
        if (a[i] != last) :
            return False;
 
    return True;
 
# Driver's Code
if __name__ == "__main__" :
 
    arr = [ 18, 30, 54, 90, 162 ];
 
    n = len(arr);
 
    # Function call to check if all
    # element in the array can be equal
    # or not.
    if (canMakeEqual(arr, n)) :
        print("YES");
     
    else :
        print("NO");
 
# This code is contributed by AnkitRai01


C#




// C# implementation to find if it's
// possible to make all elements of an
// array equal by using two operations.
using System;
 
class GFG{
 
// Function to find if it's possible
// to make all array elements equal
static bool canMakeEqual(int []a, int n)
{
    // Iterate over all numbers
    for (int i = 0; i < n; i++) {
  
        // If a number has a power of 5
        // remove it
        while (a[i] % 5 == 0) {
            a[i] /= 5;
        }
  
        // If a number has a power of 3
        // remove it
        while (a[i] % 3 == 0) {
            a[i] /= 3;
        }
    }
  
    int last = a[0];
  
    // Check if all elements are equal
    // in the final array
    for (int i = 1; i < n; i++) {
        if (a[i] != last) {
            return false;
        }
    }
  
    return true;
}
  
// Driver's Code
public static void Main(string[] args)
{
    int []arr = { 18, 30, 54, 90, 162 };
  
    int n = arr.Length;
  
    // Function call to check if all
    // element in the array can be equal
    // or not.
    if (canMakeEqual(arr, n)) {
        Console.WriteLine("YES");
    }
    else {
        Console.WriteLine("NO");
    }
}
}
 
// This code is contributed by AnkitRai01


Javascript




<script>
// javascript implementation to find if it's
// possible to make all elements of an
// array equal by using two operations.   
// Function to find if it's possible
    // to make all array elements equal
    function canMakeEqual(a , n) {
        // Iterate over all numbers
        for (i = 0; i < n; i++) {
 
            // If a number has a power of 5
            // remove it
            while (a[i] % 5 == 0) {
                a[i] /= 5;
            }
 
            // If a number has a power of 3
            // remove it
            while (a[i] % 3 == 0) {
                a[i] /= 3;
            }
        }
 
        var last = a[0];
 
        // Check if all elements are equal
        // in the final array
        for (i = 1; i < n; i++) {
            if (a[i] != last) {
                return false;
            }
        }
 
        return true;
    }
 
    // Driver's Code
     
        var arr = [ 18, 30, 54, 90, 162 ];
 
        var n = arr.length;
 
        // Function call to check if all
        // element in the array can be equal
        // or not.
        if (canMakeEqual(arr, n)) {
            document.write("YES" + "\n");
        } else {
            document.write("NO" + "\n");
        }
 
// This code contributed by umadevi9616
</script>


Output: 

YES

 

Time Complexity: O(N), where N is the size of array.

Space Complexity: O(1)
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads