Open In App

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

Last Updated : 05 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Implementation:

C++




// 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;
}


Java




// 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'.


Python3




# 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".


C#




// 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'.


PHP




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


Javascript




<script>
 
// Javascript program to check if all numbers can
// be made equal by repeated division of 2
// and 3
function canMakeEqual(a, n)
{
    for(let 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(let i = 1; i < n; i++)
        if (a[i] != a[0])
            return false;
   
    return true;
}
 
// Driver code
let A = [ 75, 150, 75, 50 ];
let n = A.length;
 
if (canMakeEqual(A, n))
    document.write("Yes");
else
    document.write("No");
     
// This code is contributed by suresh07
 
</script>


Output

Yes

Time complexity : O(n * log(max(A))), where n is the size of the input array A and max(A) is the maximum value in the array. 

Space complexity : O(1), 



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

Similar Reads