# Make parts of Array equivalent through again and again dividing parts through 2 or 3

Given an integer array A consisting of N integers. In a single transfer, we will be able to make a selection any index i ( 0 â¤ i â¤ N-1) and divide it both through 2 or 3(the quantity A[i] will have to be divisible through 2 or 3, respectively), the duty is to search out the minimal quantity of overall strikes required such that each one array parts are equivalent. If it isn’t imaginable to make all parts equivalent, print -1.

Examples:

Enter: N = 3, A[] = [1, 4, 3]
Output: 3
Rationalization: Divide A through 2 two times and A through 3 as soon as. Therefore at least 3 strikes are required.

Enter: N = 3, A[] = [2, 7, 6]
Output: -1
Rationalization: It’s not imaginable to make all array parts equivalent.

Way: To resolve the issue apply the underneath concept:

First, letâs factorize every A[i] within the type of 3p*2q*z. Now it’s simple to look that if for any i, j if the values zi and zj aren’t equivalent, then it’s unimaginable to make all of the array parts equivalent. On this case, go back -1 as the solution. Differently, we will be able to calculate the sum of all p and q over all parts and print that as the solution.

Steps that had been to apply the above manner:Â

• Allow us to to find the gcd g of the entire array and divide all numbers through g so that we have got all of the different elements as opposed to 2 or 3 separated.
• Initialize a variable ans = 0 which is able to retailer the overall collection of strikes required to make all parts equivalent.Â
• For every A[i], divide it through 2 until A[i] is divisible through 2 and increment ans = ans + 1.
• Now, repeat the above procedure, however this time divide it through 3.
• Print the general solution as ans.

Beneath is the code to put into effect the above manner:

## C++

 `#come with ` `the use of` `namespace` `std;` `Â `Â  `int` `minMoves(``int` `N, vector<``int``> a)` `{` `Â `Â  `Â Â Â Â ` `Â Â Â Â ``int` `g = 0;` `Â Â Â Â ``for` `(``int` `i = 0; i < N; i++) {` `Â Â Â Â Â Â Â Â ``g = __gcd(g, a[i]);` `Â Â Â Â ``}` `Â `Â  `Â Â Â Â ` `Â Â Â Â ` `Â Â Â Â ``int` `ans = 0;` `Â Â Â Â ``for` `(``int` `i = 0; i < N; i++) {` `Â `Â  `Â Â Â Â Â Â Â Â ` `Â Â Â Â Â Â Â Â ``a[i] /= g;` `Â `Â  `Â Â Â Â Â Â Â Â ` `Â Â Â Â Â Â Â Â ` `Â Â Â Â Â Â Â Â ``whilst` `(a[i] % 2 == 0) {` `Â Â Â Â Â Â Â Â Â Â Â Â ``a[i] /= 2;` `Â Â Â Â Â Â Â Â Â Â Â Â ``ans++;` `Â Â Â Â Â Â Â Â ``}` `Â `Â  `Â Â Â Â Â Â Â Â ` `Â Â Â Â Â Â Â Â ` `Â Â Â Â Â Â Â Â ``whilst` `(a[i] % 3 == 0) {` `Â Â Â Â Â Â Â Â Â Â Â Â ``a[i] /= 3;` `Â Â Â Â Â Â Â Â Â Â Â Â ``ans++;` `Â Â Â Â Â Â Â Â ``}` `Â `Â  `Â Â Â Â Â Â Â Â ` `Â Â Â Â Â Â Â Â ` `Â Â Â Â Â Â Â Â ``if` `(a[i] != 1) {` `Â Â Â Â Â Â Â Â Â Â Â Â ``cout << -1 << endl;` `Â Â Â Â Â Â Â Â Â Â Â Â ``go back` `0;` `Â Â Â Â Â Â Â Â ``}` `Â Â Â Â ``}` `Â `Â  `Â Â Â Â ` `Â Â Â Â ``go back` `ans;` `}` `Â `Â  `int` `major()` `{` `Â Â Â Â ``int` `N = 3;` `Â Â Â Â ``vector<``int``> a = { 1, 4, 3 };` `Â `Â  `Â Â Â Â ` `Â Â Â Â ``cout << minMoves(N, a);` `Â `Â  `Â Â Â Â ``go back` `0;` `}`

Time Complexity: O(N*max(logA[i]))
Auxiliary Area: O(1)