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++
#include <bits/stdc++.h>
using namespace std;
bool EqualNumbers( int a[], int n)
{
for ( int i = 0; i < n; i++) {
while (a[i] % 2 == 0)
a[i] /= 2;
while (a[i] % 3 == 0)
a[i] /= 3;
if (a[i] != a[0]) {
return false ;
}
}
return true ;
}
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;
}
|
Java
class GFG
{
static boolean EqualNumbers( int a[], int n)
{
for ( int i = 0 ; i < n; i++)
{
while (a[i] % 2 == 0 )
{
a[i] /= 2 ;
}
while (a[i] % 3 == 0 )
{
a[i] /= 3 ;
}
if (a[i] != a[ 0 ])
{
return false ;
}
}
return true ;
}
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" );
}
}
}
|
Python3
def EqualNumbers(a, n):
for i in range ( 0 , n):
while a[i] % 2 = = 0 :
a[i] / / = 2
while a[i] % 3 = = 0 :
a[i] / / = 3
if a[i] ! = a[ 0 ]:
return False
return True
if __name__ = = "__main__" :
a = [ 50 , 75 , 150 ]
n = len (a)
if EqualNumbers(a, n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool EqualNumbers( int []a, int n)
{
for ( int i = 0; i < n; i++)
{
while (a[i] % 2 == 0)
{
a[i] /= 2;
}
while (a[i] % 3 == 0)
{
a[i] /= 3;
}
if (a[i] != a[0])
{
return false ;
}
}
return true ;
}
public static void Main()
{
int []a = {50, 75, 150};
int n = a.Length;
if (EqualNumbers(a, n))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
PHP
<?php
function EqualNumbers( $a , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
while ( $a [ $i ] % 2 == 0)
$a [ $i ] /= 2;
while ( $a [ $i ] % 3 == 0)
$a [ $i ] /= 3;
if ( $a [ $i ] != $a [0])
{
return false;
}
}
return true;
}
$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..
?>
|
Javascript
<script>
function EqualNumbers(a, n)
{
for (let i = 0; i < n; i++)
{
while (a[i] % 2 == 0)
{
a[i] = parseInt(a[i] / 2, 10);
}
while (a[i] % 3 == 0)
{
a[i] = parseInt(a[i] / 3, 10);
}
if (a[i] != a[0])
{
return false ;
}
}
return true ;
}
let a = [ 50, 75, 150 ];
let n = a.length;
if (EqualNumbers(a, n))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Complexity Analysis:
- Time Complexity: O(nlog2m + nlog3m)
- Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
15 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...