Make all numbers of an array equal

23 Jun, 2022
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 2 or by 3. If its possible to make all the array elements equal with the given operation then print Yes else print No.
Examples: 
Input: arr[] = {50, 75, 100} Output: Yes {50 * 2 * 3, 75 * 2 * 2, 100 * 3} = {300, 300, 300}
Input: arr[] = {10, 14} Output: No 
Approach: Any positive integer number can be factorized and written as 2a * 3b * 5c * 7d * ..... We can multiply given numbers by 2 and 3 so we can increase a and b for them. So we can make all a and b equal by increasing them to the same big value (e.g. 100). But we can’t change powers of other prime numbers so they must be equal from the beginning. We can check it by diving all numbers from input by two and by three as many times as possible. Then all of them must be equal.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function that returns true if all// the array elements can be made equal// with the given operationbool EqualNumbers(int a[], int n){    for (int i = 0; i < n; i++) {         // Divide number by 2        while (a[i] % 2 == 0)            a[i] /= 2;         // Divide number by 3        while (a[i] % 3 == 0)            a[i] /= 3;         if (a[i] != a[0]) {            return false;        }    }     return true;} // Driver codeint main(){    int a[] = { 50, 75, 150 };     int n = sizeof(a) / sizeof(a[0]);     if (EqualNumbers(a, n))        cout << "Yes";    else        cout << "No";     return 0;}
// Java implementation of above approachclass GFG{     // Function that returns true if all    // the array elements can be made equal    // with the given operation    static boolean EqualNumbers(int a[], int n)    {        for (int i = 0; i < n; i++)        {             // Divide number by 2            while (a[i] % 2 == 0)            {                a[i] /= 2;            }             // Divide number by 3            while (a[i] % 3 == 0)            {                a[i] /= 3;            }             if (a[i] != a[0])            {                return false;            }        }         return true;    }     // Driver code    public static void main(String[] args)    {        int a[] = {50, 75, 150};         int n = a.length;         if (EqualNumbers(a, n))        {            System.out.println("Yes");        }        else        {            System.out.println("No");        }    }} // This code is contributed by Rajput-JI
# Python3 implementation of the approach # Function that returns true if all# the array elements can be made equal# with the given operationdef EqualNumbers(a, n):     for i in range(0, n):         # Divide number by 2        while a[i] % 2 == 0:            a[i] //= 2         # Divide number by 3        while a[i] % 3 == 0:            a[i] //= 3         if a[i] != a[0]:            return False     return True # Driver codeif __name__ == "__main__":     a = [50, 75, 150]    n = len(a)     if EqualNumbers(a, n):        print("Yes")    else:        print("No") # This code is contributed by Rituraj Jain
// C# implementation of above approachusing System; class GFG{     // Function that returns true if all    // the array elements can be made equal    // with the given operation    static bool EqualNumbers(int []a, int n)    {        for (int i = 0; i < n; i++)        {             // Divide number by 2            while (a[i] % 2 == 0)            {                a[i] /= 2;            }             // Divide number by 3            while (a[i] % 3 == 0)            {                a[i] /= 3;            }             if (a[i] != a[0])            {                return false;            }         }         return true;    }     // Driver code    public static void Main()    {        int []a = {50, 75, 150};         int n = a.Length;         if (EqualNumbers(a, n))        {            Console.WriteLine("Yes");        }        else        {            Console.WriteLine("No");        }    }} // This code is contributed by Ryuga
<?php// PHP implementation of the approach// Function that returns true if all// the array elements can be made equal// with the given operation function EqualNumbers($a, $n){    for ($i = 0; $i < $n; $i++)    {         // Divide number by 2        while ($a[$i] % 2 == 0)            $a[$i] /= 2;         // Divide number by 3        while ($a[$i] % 3 == 0)            $a[$i] /= 3;         if ($a[$i] != $a[0])        {            return false;        }    }     return true;}     // Driver code    $a = array(50, 75, 150 );    $n = sizeof($a) / sizeof($a[0]);    if (EqualNumbers($a, $n))        echo "Yes";    else        echo "No"; #This code is contributed by ajit..?>
<script> // Javascript implementation of above approach // Function that returns true if all// the array elements can be made equal// with the given operationfunction EqualNumbers(a, n){    for(let i = 0; i < n; i++)    {                 // Divide number by 2        while (a[i] % 2 == 0)        {            a[i] = parseInt(a[i] / 2, 10);        }         // Divide number by 3        while (a[i] % 3 == 0)        {            a[i] = parseInt(a[i] / 3, 10);         }         if (a[i] != a[0])        {            return false;        }    }    return true;} // Driver codelet a = [ 50, 75, 150 ];let n = a.length; if (EqualNumbers(a, n)){    document.write("Yes");}else{    document.write("No");} // This code is contributed by divyeshrabadiya07   </script>
Yes
 
Time Complexity: O(nlog2m + nlog3m)
Auxiliary Space: O(1)
Rajput-Ji
ankthon
jit_t
rituraj_jain
AshishSrivastava4
divyeshrabadiya07
rishavmahato348
number-theory
Arrays
Competitive Programming
Mathematical
Arrays
number-theory
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.