# 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `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; ` ` `  `        ``if` `(a[i] != a) { ` `            ``return` `false``; ` `        ``} ` `    ``} ` ` `  `    ``return` `true``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a[] = { 50, 75, 150 }; ` ` `  `    ``int` `n = ``sizeof``(a) / ``sizeof``(a); ` ` `  `    ``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``; ` `            ``} ` ` `  `            ``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` ` `  `        ``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;  ` `            ``}  ` ` `  `            ``if` `(a[i] != a)  ` `            ``{  ` `                ``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

 ` `

Output:

```Yes
``` My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.