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

12 Jul, 2021
Given 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[1] and arr[2] by 3, and divide arr[3] by 2. The array modifies to {15, 6, 2}.Multiply arr[1] and arr[3] by 3, divide arr[2] by 2. The array modifies to {45, 3, 6}.Multiply arr[1] by 3 and arr[2] by 3 and divide arr[3] by 2. The array modifies to {135, 9, 3}.No further operation is possible since no element is divisible by 2.
Multiply arr[1] and arr[2] by 3, and divide arr[3] by 2. The array modifies to {15, 6, 2}.
Multiply arr[1] and arr[3] by 3, divide arr[2] by 2. The array modifies to {45, 3, 6}.
Multiply arr[1] by 3 and arr[2] by 3 and divide arr[3] 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: 0
Approach: 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.
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++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to count maximum number// of multiplication by 3 or division// by 2 operations that can be performedint maximumTurns(int arr[], int N){     // Stores the maximum number    // of operations possible    int Count = 0;     // Traverse the array arr[]    for (int i = 0; i < N; i++) {         // Iterate until arr[i] is even        while (arr[i] % 2 == 0) {             // Increment count by 1            Count++;             // Update arr[i]            arr[i] = arr[i] / 2;        }    }     // Return the value of    // Count as the answer    return Count;} // Driver Codeint main(){     // Given Input    int arr[] = { 5, 2, 4 };    int M = 3, K = 2;    int N = sizeof(arr) / sizeof(arr[0]);     // Function Call    cout << maximumTurns(arr, N);     return 0;}
// Java program for the above approachpublic class GFG{     // Function to count maximum number    // of multiplication by 3 or division    // by 2 operations that can be performed    static int maximumTurns(int arr[], int N)    {         // Stores the maximum number        // of operations possible        int Count = 0;         // Traverse the array arr[]        for (int i = 0; i < N; i++) {             // Iterate until arr[i] is even            while (arr[i] % 2 == 0) {                 // Increment count by 1                Count++;                 // Update arr[i]                arr[i] = arr[i] / 2;            }        }         // Return the value of        // Count as the answer        return Count;    }     // Driver code    public static void main(String[] args)    {               // Given Input        // Given Input        int arr[] = { 5, 2, 4 };        int M = 3, K = 2;        int N = arr.length;         // Function Call        System.out.println(maximumTurns(arr, N));    }} // This code is contributed by abhinavjain194
# Python3 program for the above approach # Function to count maximum number# of multiplication by 3 or division# by 2 operations that can be performeddef maximumTurns(arr, N):         # Stores the maximum number    # of operations possible    Count = 0         # Traverse the array arr[]    for i in range(0, N):                 # Iterate until arr[i] is even        while (arr[i] % 2 == 0):                         # Increment count by 1            Count += 1                         # Update arr[i]            arr[i] = arr[i] // 2     # Return the value of    # Count as the answer    return Count # Driver code # Given Inputarr = [ 5, 2, 4 ]M = 3K = 2N = len(arr) # Function Callprint(maximumTurns(arr, N)) # This code is contributed by amreshkumar3
// C# program for the above approach using System;using System.Collections.Generic; class GFG{ // Function to count maximum number// of multiplication by 3 or division// by 2 operations that can be performedstatic int maximumTurns(int []arr, int N){     // Stores the maximum number    // of operations possible    int Count = 0;     // Traverse the array arr[]    for (int i = 0; i < N; i++) {         // Iterate until arr[i] is even        while (arr[i] % 2 == 0) {             // Increment count by 1            Count++;             // Update arr[i]            arr[i] = arr[i] / 2;        }    }     // Return the value of    // Count as the answer    return Count;} // Driver Codepublic static void Main(){     // Given Input    int []arr = { 5, 2, 4 };    int N = arr.Length;     // Function Call    Console.Write(maximumTurns(arr, N));}} // This code is contributed by ipg2016107.
<script> // JavaScript program for the above approach // Function to count maximum number// of multiplication by 3 or division// by 2 operations that can be performedfunction maximumTurns(arr, N) {     // Stores the maximum number    // of operations possible    let Count = 0;     // Traverse the array arr[]    for (let i = 0; i < N; i++) {         // Iterate until arr[i] is even        while (arr[i] % 2 == 0) {             // Increment count by 1            Count++;             // Update arr[i]            arr[i] = Math.floor(arr[i] / 2);        }    }     // Return the value of    // Count as the answer    return Count;} // Driver Code  // Given Inputlet arr = [5, 2, 4];let M = 3, K = 2;let N = arr.length; // Function Calldocument.write(maximumTurns(arr, N)); </script>
3
 
Time complexity: O(N*log(M)) where M is the maximum value of the array.Auxiliary Space: O(1) 
amreshkumar3
_saurabh_jaiswal
abhinavjain194
ipg2016107
Arrays
Mathematical
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count pairs with given sum
Chocolate Distribution Problem
Window Sliding Technique
Reversal algorithm for array rotation
Next Greater Element
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7