Techno Blender
Digitally Yours.

Make elements of Array equal by repeatedly dividing elements by 2 or 3

0 40


Improve Article

Save Article

Like Article

Improve Article

Save Article

Like Article

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;

}

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

Like Article

Save Article


Improve Article

Save Article

Like Article

Improve Article

Save Article

Like Article

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;

}

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

Like Article

Save Article

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment