Wednesday, 27 Oct 2021

# Maximum number of multiplication by 3 or division by 2 operations possible on an array

Maximum number of multiplication by 3 or division by 2 operations possible on an arrayGiven an array arr[] consisting of N positive integers, the task is to find the maximum number of times each array element can either be multiplied by M or divided by K. Note: At least one element needs to be divided by M and K respectively in each operation.Examples:Input: arr[] = {5, 2, 4}, M = 3, K = 2Output: 3Explanation:One possible way to perform the operations is:Multiply arr and arr by 3, and divide arr by 2. The array modifies to {15, 6, 2}.Multiply arr and arr by 3, divide arr by 2. The array modifies to {45, 3, 6}.Multiply arr by 3 and arr by 3 and divide arr by 2. The array modifies to {135, 9, 3}.No further operation is possible since no element is divisible by 2.Therefore, the maximum number of operations possible is 3.Input: arr[] = {3, 5, 7}Output: 0Approach: This problem can be solved by observing that, successively dividing an array element by 2, the count of even elements will decrease after some constant number of steps. So, to maximize the number of turns, only one even element is divided by 2, and all others are multiplied by 3 in a single step. Follow the steps below to solve the problem:Initialize a variable, say, Count as 0, that will store the count of power of 2 in every element of the array.Iterate in the range [0, N-1] using the variable i and perform the following steps:Iterate until the arr[i] is divisible by 2, then increment the Count by 1 and divide arr[i] by 2.After performing the above steps, print the value of Count as the answer.Below is the implementation of the above approach:C++#include using namespace std;int maximumTurns(int arr[], int N){            int Count = 0;        for (int i = 0; i < N; i++) {                while (arr[i] % 2 == 0) {                        Count++;                        arr[i] = arr[i] / 2;        }    }            return Count;}int main(){        int arr[] = { 5, 2, 4 };    int M = 3, K = 2;    int N = sizeof(arr) / sizeof(arr);        cout