Check if it is possible to make array equal by doubling or tripling
Last Updated :
05 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
bool canMakeEqual( int a[], int n)
{
for ( int i = 0; i < n; i++) {
while (a[i] % 2 == 0)
a[i] = a[i] / 2;
while (a[i] % 3 == 0)
a[i] = a[i] / 3;
}
for ( int i = 1; i < n; i++)
if (a[i] != a[0])
return false ;
return true ;
}
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
import java.util.*;
class GFG {
static Boolean canMakeEqual( int a[], int n)
{
for ( int i = 0 ; i < n; i++) {
while (a[i] % 2 == 0 )
a[i] = a[i] / 2 ;
while (a[i] % 3 == 0 )
a[i] = a[i] / 3 ;
}
for ( int i = 1 ; i < n; i++)
if (a[i] != a[ 0 ])
return false ;
return true ;
}
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" );
}
}
|
Python3
def canMakeEqual( a , n ):
for i in range (n):
while a[i] % 2 = = 0 :
a[i] = int (a[i] / 2 )
while a[i] % 3 = = 0 :
a[i] = int (a[i] / 3 )
for i in range ( 1 ,n):
if a[i] ! = a[ 0 ]:
return False
return True
A = [ 75 , 150 , 75 , 50 ]
n = len (A)
print ( "Yes" if canMakeEqual(A, n) else "No" )
|
C#
using System;
class GFG {
static Boolean canMakeEqual( int []a, int n)
{
for ( int i = 0; i < n; i++) {
while (a[i] % 2 == 0)
a[i] = a[i] / 2;
while (a[i] % 3 == 0)
a[i] = a[i] / 3;
}
for ( int i = 1; i < n; i++)
if (a[i] != a[0])
return false ;
return true ;
}
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" );
}
}
|
PHP
<?php
function canMakeEqual( $a , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
while ( $a [ $i ] % 2 == 0)
$a [ $i ] = $a [ $i ] / 2;
while ( $a [ $i ] % 3 == 0)
$a [ $i ] = $a [ $i ] / 3;
}
for ( $i = 1; $i < $n ; $i ++)
if ( $a [ $i ] != $a [0])
return false;
return true;
}
$A = array (75, 150, 75, 50);
$n = sizeof( $A );
if (canMakeEqual( $A , $n ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function canMakeEqual(a, n)
{
for (let i = 0; i < n; i++)
{
while (a[i] % 2 == 0)
a[i] = a[i] / 2;
while (a[i] % 3 == 0)
a[i] = a[i] / 3;
}
for (let i = 1; i < n; i++)
if (a[i] != a[0])
return false ;
return true ;
}
let A = [ 75, 150, 75, 50 ];
let n = A.length;
if (canMakeEqual(A, n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
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),
Share your thoughts in the comments
Please Login to comment...