Check if it is possible to make array equal by doubling or tripling

Given an array of n elements.You can double or triple the elements in the array any number of times. After all the operations check whether it is possible to make all elements in the array equal.

Examples :

Input : A[] = {75, 150, 75, 50}
Output : Yes
Explanation : Here, 75 should be doubled twice and 
150 should be doubled once and 50 should be doubled
once and tripled once.Then, all the elements will 
be equal to 300.

Input : A[] = {100, 151, 200}
Output : No
Explanation : No matter what we do all elements in
the array could not be equal.
          

The idea is to repeatedly divide every element by 2 and 3 until the element is divisible. After this step, if all elements become same, then answer is yes.

How does this work? We know that every integer can be expressed as product of prime numbers 2a.3b.5c.7d…... So, in our problem we can increase a and b by doubling(*2) or tripling(*3). We can make a and b of all elements in the array equal by multiplying with 2 or 3. But the numbers also have other prime numbers in their product representation, we cannot change the powers of them. So to make all numbers equal they should have powers on other prime numbers equal from the beginning. We can check it by dividing all the numbers by two or three as many times as possible. Then all of them should be equal.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if all numbers can
// be made equal by repeated division of 2
// and 3
#include <bits/stdc++.h>
using namespace std;
  
bool canMakeEqual(int a[], int n)
{
    for (int i = 0; i < n; i++) {
  
        // continuously divide every number by 2  
        while (a[i] % 2 == 0) 
            a[i] = a[i] / 2;
  
       // continuously divide every number by 3
        while (a[i] % 3 == 0)
            a[i] = a[i] / 3;
    }
  
    // Check if all numbers same
    for (int i = 1; i < n; i++) 
        if (a[i] != a[0]) 
           return false;
  
    return true;
}
  
// Driver Code
int main()
{
    int A[] = { 75, 150, 75, 50 };
    int n = sizeof(A) / sizeof(A[0]);
    if (canMakeEqual(A, n))
       cout << "Yes";
    else
       cout << "No";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if all numbers can
// be made equal by repeated division of 2
// and 3
import java.util.*;
  
class GFG {
  
static Boolean canMakeEqual(int a[], int n)
{
    for (int i = 0; i < n; i++) {
  
        // Continuously divide every number by 2 
        while (a[i] % 2 == 0
            a[i] = a[i] / 2;
  
    // Continuously divide every number by 3
        while (a[i] % 3 == 0)
            a[i] = a[i] / 3;
    }
  
    // Check if all numbers same
    for (int i = 1; i < n; i++) 
        if (a[i] != a[0]) 
        return false;
  
    return true;
}
  
// Driver Code
public static void main(String[] args)
{
int A[] = { 75, 150, 75, 50 };
    int n = A.length;
    if (canMakeEqual(A, n))
        System.out.print("Yes");
    else
        System.out.print("No");
}
    }
  
// This code is contributed by 'Gitanjali'.

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to check if all numbers can
# be made equal by repeated division of 2
# and 3
  
def canMakeEqual( a , n ):
    for i in range(n):
  
        # continuously divide every number by 2
        while a[i] % 2 == 0:
            a[i] = int(a[i] / 2)
          
        # continuously divide every number by 3
        while a[i] % 3 == 0:
            a[i] = int(a[i] / 3)
      
    # Check if all numbers same
    for i in range(1,n):
        if a[i] != a[0]:
            return False
      
    return True
      
# Driver Code
A = [ 75, 150, 75, 50 ]
n = len(A)
print("Yes" if canMakeEqual(A, n) else "No")
  
# This code is contributed by "Sharad_Bhardwaj".

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if all numbers can
// be made equal by repeated division of 2
// and 3
using System;
  
class GFG {
  
    static Boolean canMakeEqual(int []a, int n)
    {
        for (int i = 0; i < n; i++) {
      
            // Continuously divide every number by 2 
            while (a[i] % 2 == 0) 
                a[i] = a[i] / 2;
      
            // Continuously divide every number by 3
            while (a[i] % 3 == 0)
                a[i] = a[i] / 3;
        }
      
        // Check if all numbers same
        for (int i = 1; i < n; i++) 
            if (a[i] != a[0]) 
                return false;
      
        return true;
    }
      
    // Driver Code
    public static void Main()
    {
          
        int []A = { 75, 150, 75, 50 };
        int n = A.Length;
          
        if (canMakeEqual(A, n))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
  
// This code is contributed by 'vt_m'.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check if 
// all numbers can be made 
// equal by repeated division 
// of 2 and 3
  
function canMakeEqual($a, $n)
{
    for ($i = 0; $i < $n; $i++)
    {
  
        // continuously divide 
        // every number by 2 
        while ($a[$i] % 2 == 0) 
            $a[$i] = $a[$i] / 2;
  
    // continuously divide
    // every number by 3
        while ($a[$i] % 3 == 0)
            $a[$i] = $a[$i] / 3;
    }
  
    // Check if all numbers same
    for ($i = 1; $i < $n; $i++) 
        if ($a[$i] != $a[0]) 
        return false;
  
    return true;
}
  
// Driver Code
$A = array(75, 150, 75, 50);
$n = sizeof($A);
if (canMakeEqual($A, $n))
    echo "Yes";
else
    echo "No";
  
// This code is contributed by aj_36
?>

chevron_right



Output :

  Yes


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : jit_t



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.