Make all numbers of an array equal

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 operation
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
int main()
{
    int a[] = { 50, 75, 150 };
  
    int n = sizeof(a) / sizeof(a[0]);
  
    if (EqualNumbers(a, n))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach
class 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function that returns true if all 
# the array elements can be made equal 
# with the given operation 
def 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 code 
if __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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above approach 
using 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 

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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..
?>

chevron_right


Output:

Yes


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.