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 else print No.

**Examples:**

Input:arr[] = {18, 30, 54, 90, 162}

Output:Yes

Explanation:

We can perform following operations:

162 X 5 = 810

90 X 3 X 3 = 810

54 X 5 X 3 = 810

30 X 3 X 3 X 3 = 810

18 X 5 X 3 X 3 = 810

Input:arr[] = {18, 36, 58, 90, 162}

Output:No

Explanation:

There is no way you can make all elements equal.

**Observations**:

- If after some operations, all the numbers become equal then they will have the same Prime factorization i.e each number will have the same power of
**2, 3, 5…**and so on. - Since we are multiplying the numbers only by
**3**and**5**which are Prime Numbers so we can make powers of**3 and 5**in the prime factorization of all numbers equal after some operations. - Therefore, for all numbers to be made equal, the powers of Prime Numbers in the prime factorization other than
**3**and**5**must be equal. - The solution would be to take each number and remove all powers of
**3**and**5**from it. If then all numbers turn out to be equal then it’s possible to make array elements equal by using the given operations otherwise it’s not possible.

**Steps**:

- Divide each element of array
**arr[]**with 3 and 5 such that all the power of**3**and**5**in Prime Factorization of each element becomes zero. - Check if all the element of the array are equal or not. If yes then print Yes.
- Else print No.

Below is the implementation of the above approach:

## CPP

`// C++ implementation to find if it's ` `// possible to make all elements of an ` `// array equal by using two operations. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find if it's possible ` `// to make all array elements equal ` `bool` `canMakeEqual(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// Iterate over all numbers ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// If a number has a power of 5 ` ` ` `// remove it ` ` ` `while` `(a[i] % 5 == 0) { ` ` ` `a[i] /= 5; ` ` ` `} ` ` ` ` ` `// If a number has a power of 3 ` ` ` `// remove it ` ` ` `while` `(a[i] % 3 == 0) { ` ` ` `a[i] /= 3; ` ` ` `} ` ` ` `} ` ` ` ` ` `int` `last = a[0]; ` ` ` ` ` `// Check if all elements are equal ` ` ` `// in the final array ` ` ` `for` `(` `int` `i = 1; i < n; i++) { ` ` ` `if` `(a[i] != last) { ` ` ` `return` `false` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Driver's Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 18, 30, 54, 90, 162 }; ` ` ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `// Function call to check if all ` ` ` `// element in the array can be equal ` ` ` `// or not. ` ` ` `if` `(canMakeEqual(arr, n)) { ` ` ` `cout << ` `"YES"` `<< endl; ` ` ` `} ` ` ` `else` `{ ` ` ` `cout << ` `"NO"` `<< endl; ` ` ` `} ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find if it's ` `// possible to make all elements of an ` `// array equal by using two operations. ` `class` `GFG{ ` ` ` `// Function to find if it's possible ` `// to make all array elements equal ` `static` `boolean` `canMakeEqual(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// Iterate over all numbers ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` ` ` `// If a number has a power of 5 ` ` ` `// remove it ` ` ` `while` `(a[i] % ` `5` `== ` `0` `) { ` ` ` `a[i] /= ` `5` `; ` ` ` `} ` ` ` ` ` `// If a number has a power of 3 ` ` ` `// remove it ` ` ` `while` `(a[i] % ` `3` `== ` `0` `) { ` ` ` `a[i] /= ` `3` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `int` `last = a[` `0` `]; ` ` ` ` ` `// Check if all elements are equal ` ` ` `// in the final array ` ` ` `for` `(` `int` `i = ` `1` `; i < n; i++) { ` ` ` `if` `(a[i] != last) { ` ` ` `return` `false` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Driver's Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `18` `, ` `30` `, ` `54` `, ` `90` `, ` `162` `}; ` ` ` ` ` `int` `n = arr.length; ` ` ` ` ` `// Function call to check if all ` ` ` `// element in the array can be equal ` ` ` `// or not. ` ` ` `if` `(canMakeEqual(arr, n)) { ` ` ` `System.out.print(` `"YES"` `+` `"\n"` `); ` ` ` `} ` ` ` `else` `{ ` ` ` `System.out.print(` `"NO"` `+` `"\n"` `); ` ` ` `} ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find if it's ` `# possible to make all elements of an ` `# array equal by using two operations. ` ` ` `# Function to find if it's possible ` `# to make all array elements equal ` `def` `canMakeEqual( a, n) : ` ` ` ` ` `# Iterate over all numbers ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` ` ` `# If a number has a power of 5 ` ` ` `# remove it ` ` ` `while` `(a[i] ` `%` `5` `=` `=` `0` `) : ` ` ` `a[i] ` `/` `/` `=` `5` `; ` ` ` ` ` `# If a number has a power of 3 ` ` ` `# remove it ` ` ` `while` `(a[i] ` `%` `3` `=` `=` `0` `) : ` ` ` `a[i] ` `/` `/` `=` `3` `; ` ` ` ` ` `last ` `=` `a[` `0` `]; ` ` ` ` ` `# Check if all elements are equal ` ` ` `# in the final array ` ` ` `for` `i ` `in` `range` `(` `1` `,n) : ` ` ` `if` `(a[i] !` `=` `last) : ` ` ` `return` `False` `; ` ` ` ` ` `return` `True` `; ` ` ` `# Driver's Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[ ` `18` `, ` `30` `, ` `54` `, ` `90` `, ` `162` `]; ` ` ` ` ` `n ` `=` `len` `(arr); ` ` ` ` ` `# Function call to check if all ` ` ` `# element in the array can be equal ` ` ` `# or not. ` ` ` `if` `(canMakeEqual(arr, n)) : ` ` ` `print` `(` `"YES"` `); ` ` ` ` ` `else` `: ` ` ` `print` `(` `"NO"` `); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find if it's ` `// possible to make all elements of an ` `// array equal by using two operations. ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find if it's possible ` `// to make all array elements equal ` `static` `bool` `canMakeEqual(` `int` `[]a, ` `int` `n) ` `{ ` ` ` `// Iterate over all numbers ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// If a number has a power of 5 ` ` ` `// remove it ` ` ` `while` `(a[i] % 5 == 0) { ` ` ` `a[i] /= 5; ` ` ` `} ` ` ` ` ` `// If a number has a power of 3 ` ` ` `// remove it ` ` ` `while` `(a[i] % 3 == 0) { ` ` ` `a[i] /= 3; ` ` ` `} ` ` ` `} ` ` ` ` ` `int` `last = a[0]; ` ` ` ` ` `// Check if all elements are equal ` ` ` `// in the final array ` ` ` `for` `(` `int` `i = 1; i < n; i++) { ` ` ` `if` `(a[i] != last) { ` ` ` `return` `false` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Driver's Code ` `public` `static` `void` `Main(` `string` `[] args) ` `{ ` ` ` `int` `[]arr = { 18, 30, 54, 90, 162 }; ` ` ` ` ` `int` `n = arr.Length; ` ` ` ` ` `// Function call to check if all ` ` ` `// element in the array can be equal ` ` ` `// or not. ` ` ` `if` `(canMakeEqual(arr, n)) { ` ` ` `Console.WriteLine(` `"YES"` `); ` ` ` `} ` ` ` `else` `{ ` ` ` `Console.WriteLine(` `"NO"` `); ` ` ` `} ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

YES

**Time Complexity:** O(N), where N is the size of array.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the number of operations required to make all array elements Equal
- Find the minimum number of operations required to make all array elements equal
- Minimum Cost to make all array elements equal using given operations
- Minimum number of increment-other operations to make all array elements equal.
- Minimum operations required to make all the array elements equal
- Minimum operations to make all elements equal using the second array
- Count of operations to make all elements of array a[] equal to its min element by performing a[i] – b[i]
- Minimum increment by k operations to make all elements equal
- Make all the array elements odd with minimum operations of given type
- Make all the elements of array even with given operations
- Minimum Bitwise AND operations to make any two array elements equal
- Minimum Bitwise OR operations to make any two array elements equal
- Minimum Bitwise XOR operations to make any two array elements equal
- Minimum decrement operations to make Array elements equal by only decreasing K each time
- Check if its possible to make sum of the array odd with given Operations
- Make all elements of an array equal with the given operation
- Minimum delete operations to make all elements of array same
- Minimum gcd operations to make all array elements one
- Minimum operations required to make all Array elements divisible by K
- Minimum no. of operations required to make all Array Elements Zero

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.