Make elements of Array equal by repeatedly dividing elements by 2 or 3
Last Updated :
07 May, 2023
Given an integer array A consisting of N integers. In one move, we can choose any index i ( 0 ≤ i ≤ N-1) and divide it either by 2 or 3(the number A[i] should be divisible by 2 or 3, respectively), the task is to find the minimum number of total moves required such that all array elements are equal. If it is not possible to make all elements equal, print -1.
Examples:
Input: N = 3, A[] = [1, 4, 3]
Output: 3
Explanation: Divide A[1] by 2 twice and A[2] by 3 once. Hence a minimum of 3 moves are required.
Input: N = 3, A[] = [2, 7, 6]
Output: -1
Explanation: It is not possible to make all array elements equal.
Approach: To solve the problem follow the below idea:
First, let’s factorize each A[i] in the form of 3p*2q*z. Now it is easy to see that if for any i, j if the values zi and zj are not equal, then it is impossible to make all the array elements equal. In this case, return -1 as the answer. Otherwise, we can calculate the sum of all p and q over all elements and print that as the answer.
Steps that were to follow the above approach:
- Let us find the gcd g of the whole array and divide all numbers by g so that we have all the other factors other than 2 or 3 separated.
- Initialize a variable ans = 0 which will store the total number of moves required to make all elements equal.
- For each A[i], divide it by 2 till A[i] is divisible by 2 and increment ans = ans + 1.
- Now, repeat the above process, but this time divide it by 3.
- Print the final answer as ans.
Below is the code to implement the above approach:
C++
#include <bits/stdc++.h>
using 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;
while (a[i] % 2 == 0) {
a[i] /= 2;
ans++;
}
while (a[i] % 3 == 0) {
a[i] /= 3;
ans++;
}
if (a[i] != 1) {
cout << -1 << endl;
return 0;
}
}
return ans;
}
int main()
{
int N = 3;
vector< int > a = { 1, 4, 3 };
cout << minMoves(N, a);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int minMoves( int N, ArrayList<Integer> a) {
int g = 0 ;
for ( int i = 0 ; i < N; i++) {
g = gcd(g, a.get(i));
}
int ans = 0 ;
for ( int i = 0 ; i < N; i++) {
a.set(i, a.get(i) / g);
while (a.get(i) % 2 == 0 ) {
a.set(i, a.get(i) / 2 );
ans++;
}
while (a.get(i) % 3 == 0 ) {
a.set(i, a.get(i) / 3 );
ans++;
}
if (a.get(i) != 1 ) {
System.out.println(- 1 );
return 0 ;
}
}
return ans;
}
public static int gcd( int a, int b) {
if (b == 0 ) {
return a;
} else {
return gcd(b, a % b);
}
}
public static void main(String[] args) {
int N = 3 ;
ArrayList<Integer> a = new ArrayList<>(Arrays.asList( 1 , 4 , 3 ));
System.out.println(minMoves(N, a));
}
}
|
Python3
import math
def minMoves(N, a):
g = 0
for i in range (N):
g = math.gcd(g, a[i])
ans = 0
for i in range (N):
a[i] / = g
while (a[i] % 2 = = 0 ):
a[i] / = 2
ans + = 1
while (a[i] % 3 = = 0 ):
a[i] / = 3
ans + = 1
if (a[i] ! = 1 ):
print ( - 1 )
return 0
return ans
if __name__ = = '__main__' :
N = 3
a = [ 1 , 4 , 3 ]
print (minMoves(N, a))
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int minMoves( int N, List< 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;
while (a[i] % 2 == 0)
{
a[i] /= 2;
ans++;
}
while (a[i] % 3 == 0)
{
a[i] /= 3;
ans++;
}
if (a[i] != 1)
{
Console.WriteLine( "-1" );
return 0;
}
}
return ans;
}
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void Main( string [] args)
{
int N = 3;
List< int > a = new List< int > { 1, 4, 3 };
Console.WriteLine(minMoves(N, a));
}
}
|
Javascript
function minMoves(N, a) {
let g = 0;
for (let i = 0; i < N; i++) {
g = gcd(g, a[i]);
}
let ans = 0;
for (let i = 0; i < N; i++) {
a[i] /= g;
while (a[i] % 2 == 0) {
a[i] /= 2;
ans++;
}
while (a[i] % 3 == 0) {
a[i] /= 3;
ans++;
}
if (a[i] != 1) {
console.log(-1);
return 0;
}
}
return ans;
}
let N = 3;
let a = [ 1, 4, 3 ];
console.log(minMoves(N, a));
|
Time Complexity: O(N*max(logA[i]))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...