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++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if all // the array elements can be made equal // with the given operation bool EqualNumbers(int a[], int n) { for (int i = 0; i < n; i++) { // Divide number by 2 while (a[i] % 2 == 0) a[i] /= 2; // Divide number by 3 while (a[i] % 3 == 0) a[i] /= 3; } // Remaining numbers for (int i = 1; i < n; i++) if (a[i] != a[0]) { return false; } return true; } // Driver code 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
// Java implementation of above approach class GFG { // Function that returns true if all // the array elements can be made equal // with the given operation static boolean EqualNumbers(int a[], int n) { for (int i = 0; i < n; i++) { // Divide number by 2 while (a[i] % 2 == 0) { a[i] /= 2; } // Divide number by 3 while (a[i] % 3 == 0) { a[i] /= 3; } } // Remaining numbers 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[] = {50, 75, 150}; int n = a.length; if (EqualNumbers(a, n)) { System.out.println("Yes"); } else { System.out.println("No"); } } } // This code is contributed by Rajput-JI |
Python3
# Python3 implementation of the approach # Function that returns true if all # the array elements can be made equal # with the given operation def EqualNumbers(a, n): for i in range(0, n): # Divide number by 2 while a[i] % 2 == 0: a[i] //= 2 # Divide number by 3 while a[i] % 3 == 0: a[i] //= 3 # Remaining numbers for i in range(1, n): if a[i] != a[0]: return False return True # Driver code if __name__ == "__main__": a = [50, 75, 150] n = len(a) if EqualNumbers(a, n): print("Yes") else: print("No") # This code is contributed by Rituraj Jain |
C#
// C# implementation of above approach using System; class GFG { // Function that returns true if all // the array elements can be made equal // with the given operation static bool EqualNumbers(int []a, int n) { for (int i = 0; i < n; i++) { // Divide number by 2 while (a[i] % 2 == 0) { a[i] /= 2; } // Divide number by 3 while (a[i] % 3 == 0) { a[i] /= 3; } } // Remaining numbers for (int i = 1; i < n; i++) { if (a[i] != a[0]) { return false; } } return true; } // Driver code public static void Main() { int []a = {50, 75, 150}; int n = a.Length; if (EqualNumbers(a, n)) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } } // This code is contributed by Ryuga |
PHP
<?php // PHP implementation of the approach // Function that returns true if all // the array elements can be made equal // with the given operation function EqualNumbers($a, $n) { for ($i = 0; $i < $n; $i++) { // Divide number by 2 while ($a[$i] % 2 == 0) $a[$i] /= 2; // Divide number by 3 while ($a[$i] % 3 == 0) $a[$i] /= 3; } // Remaining numbers for ($i = 1; $i < $n; $i++) if ($a[$i] != $a[0]) { return false; } return true; } // Driver code $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.. ?> |
Yes
Recommended Posts:
- Minimum multiplications with {2, 3, 7} to make two numbers equal
- Make all elements of an array equal with the given operation
- Check if it is possible to make array equal by doubling or tripling
- Make all array elements equal with minimum cost
- Minimum operation to make all elements equal in array
- Minimum operations required to make all the array elements equal
- Minimum Bitwise AND operations to make any two array elements equal
- Minimum Increment / decrement to make array elements equal
- Find the number of operations required to make all array elements Equal
- Minimum number of increment-other operations to make all array elements equal.
- Maximum count of equal numbers in an array after performing given operations
- Check if elements of array can be made equal by multiplying given prime numbers
- Minimum operations to make the MEX of the given set equal to x
- Minimum increment by k operations to make all elements equal
- Minimum number of moves to make all elements equal
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.



