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++
// 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>
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),
Similar Reads
Check whether it is possible to make both arrays equal by modifying a single element Given two sequences of integers 'A' and 'B', and an integer 'k'. The task is to check if we can make both sequences equal by modifying any one element from the sequence A in the following way: We can add any number from the range [-k, k] to any element of A. This operation must only be performed onc
11 min read
Check if it is possible to make all strings of A[] equal to B[] using given operations Consider two arrays, A[] and B[], each containing N strings. These strings are composed solely of digits ranging from 0 to 9. Then your task is to output YES or NO, by following that all the strings of A[] can be made equal to B[] for each i (1 <= i <= N) in at most K cost. The following opera
9 min read
Check if possible to make Array sum equal to Array product by replacing exactly one element Given an array arr[] consisting of N non-negative integers, the task is to check if it is possible to make the sum of the array equal to the product of the array element by replacing exactly one array element with any non-negative integer. Examples: Input: arr[] = {1, 3, 4}Output: YesExplanation:Rep
7 min read
Find if it is possible to make all elements of an array equal by the given operations 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 3 or by 5 any number of times. If it's possible to make all the array elements equal with the given operation then print Yes el
7 min read
Check whether it is possible to convert A into B Given two integers A and B. The task is to check whether it is possible to convert A into B by performing below operations any number of times. Convert current number x to 2 * x.Convert current number x to (10 * x) + 1.Examples: Input: A = 2, B = 82 Output: Yes 2 -> 4 -> 41 -> 82Input: A =
9 min read