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++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count moves required to
// make all elements of array equal.
int minMoves(int N, vector<int> a)
{
// Calculating gcd of the array.
int g = 0;
for (int i = 0; i < N; i++) {
g = __gcd(g, a[i]);
}
// Initializing a variable to
// store the answer.
int ans = 0;
for (int i = 0; i < N; i++) {
// Dividing by gcd of array
a[i] /= g;
// Counting moves while dividing
// by 2.
while (a[i] % 2 == 0) {
a[i] /= 2;
ans++;
}
// Counting moves while dividing
// by 3.
while (a[i] % 3 == 0) {
a[i] /= 3;
ans++;
}
// Checkinng if A[i] is != -1 after
// applying the above process.
if (a[i] != 1) {
cout << -1 << endl;
return 0;
}
}
// Returning the answer.
return ans;
}
// Driver Code
int main()
{
int N = 3;
vector<int> a = { 1, 4, 3 };
// Function call
cout << minMoves(N, a);
return 0;
}
import java.util.*;
public class Main {
// Function to count moves required to
// make all elements of array equal.
public static int minMoves(int N, ArrayList<Integer> a) {
// Calculating gcd of the array.
int g = 0;
for (int i = 0; i < N; i++) {
g = gcd(g, a.get(i));
}
// Initializing a variable to
// store the answer.
int ans = 0;
for (int i = 0; i < N; i++) {
// Dividing by gcd of array
a.set(i, a.get(i) / g);
// Counting moves while dividing
// by 2.
while (a.get(i) % 2 == 0) {
a.set(i, a.get(i) / 2);
ans++;
}
// Counting moves while dividing
// by 3.
while (a.get(i) % 3 == 0) {
a.set(i, a.get(i) / 3);
ans++;
}
// Checkinng if A[i] is != -1 after
// applying the above process.
if (a.get(i) != 1) {
System.out.println(-1);
return 0;
}
}
// Returning the answer.
return ans;
}
// Function to calculate gcd of two numbers.
public static int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
// Driver Code
public static void main(String[] args) {
int N = 3;
ArrayList<Integer> a = new ArrayList<>(Arrays.asList(1, 4, 3));
// Function call
System.out.println(minMoves(N, a));
}
}
// This code is contributed by Akash Jha
# Python program for the above approach
import math
# Function to count moves required to
# make all elements of array equal.
def minMoves(N, a):
# Calculating gcd of the array.
g = 0
for i in range(N):
g = math.gcd(g, a[i])
# Initializing a variable to
# store the answer.
ans = 0
for i in range(N):
# Dividing by gcd of array
a[i] /= g
# Counting moves while dividing
# by 2.
while (a[i] % 2 == 0):
a[i] /= 2
ans += 1
# Counting moves while dividing
# by 3.
while (a[i] % 3 == 0):
a[i] /= 3
ans += 1
# Checkinng if A[i] is != -1 after
# applying the above process.
if (a[i] != 1):
print(-1)
return 0
# Returning the answer.
return ans
# Driver Code
if __name__ == '__main__':
N = 3
a = [1, 4, 3]
# Function call
print(minMoves(N, a))
# This code is contributed by Tapesh(tapeshdua420)
using System;
using System.Collections.Generic;
class Program
{
// Function to count moves required to
// make all elements of array equal.
static int minMoves(int N, List<int> a)
{
// Calculating gcd of the array.
int g = 0;
for (int i = 0; i < N; i++)
{
g = gcd(g, a[i]);
}
// Initializing a variable to
// store the answer.
int ans = 0;
for (int i = 0; i < N; i++)
{
// Dividing by gcd of array
a[i] /= g;
// Counting moves while dividing
// by 2.
while (a[i] % 2 == 0)
{
a[i] /= 2;
ans++;
}
// Counting moves while dividing
// by 3.
while (a[i] % 3 == 0)
{
a[i] /= 3;
ans++;
}
// Checkinng if A[i] is != -1 after
// applying the above process.
if (a[i] != 1)
{
Console.WriteLine("-1");
return 0;
}
}
// Returning the answer.
return ans;
}
// Function to calculate gcd of two numbers.
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Driver Code
static void Main(string[] args)
{
int N = 3;
List<int> a = new List<int> { 1, 4, 3 };
// Function call
Console.WriteLine(minMoves(N, a));
}
}
// This code is contributed by Akash Jha
// Function to count moves required to
// make all elements of array equal.
function minMoves(N, a) {
// Calculating gcd of the array.
let g = 0;
for (let i = 0; i < N; i++) {
g = gcd(g, a[i]);
}
// Initializing a variable to
// store the answer.
let ans = 0;
for (let i = 0; i < N; i++) {
// Dividing by gcd of array
a[i] /= g;
// Counting moves while dividing
// by 2.
while (a[i] % 2 == 0) {
a[i] /= 2;
ans++;
}
// Counting moves while dividing
// by 3.
while (a[i] % 3 == 0) {
a[i] /= 3;
ans++;
}
// Checkinng if A[i] is != -1 after
// applying the above process.
if (a[i] != 1) {
console.log(-1);
return 0;
}
}
// Returning the answer.
return ans;
}
// Driver Code
let N = 3;
let a = [ 1, 4, 3 ];
// Function call
console.log(minMoves(N, a));
// This code is contributed by Akash Jha
Output
3
Time Complexity: O(N*max(logA[i]))
Auxiliary Space: O(1)