Open In App

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

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
 

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: 
 




// 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 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 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# 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




<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)
 


Article Tags :