Maximum number of multiplication by 3 or division by 2 operations possible on an array
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 = 2
Output: 3
Explanation:
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.
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.
- After performing the above steps, print the value of Count as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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[0]);
cout << maximumTurns(arr, N);
return 0;
}
|
Java
public class GFG
{
static 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;
}
public static void main(String[] args)
{
int arr[] = { 5 , 2 , 4 };
int M = 3 , K = 2 ;
int N = arr.length;
System.out.println(maximumTurns(arr, N));
}
}
|
Python3
def maximumTurns(arr, N):
Count = 0
for i in range ( 0 , N):
while (arr[i] % 2 = = 0 ):
Count + = 1
arr[i] = arr[i] / / 2
return Count
arr = [ 5 , 2 , 4 ]
M = 3
K = 2
N = len (arr)
print (maximumTurns(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static 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;
}
public static void Main()
{
int []arr = { 5, 2, 4 };
int N = arr.Length;
Console.Write(maximumTurns(arr, N));
}
}
|
Javascript
<script>
function maximumTurns(arr, N) {
let Count = 0;
for (let i = 0; i < N; i++) {
while (arr[i] % 2 == 0) {
Count++;
arr[i] = Math.floor(arr[i] / 2);
}
}
return Count;
}
let arr = [5, 2, 4];
let M = 3, K = 2;
let N = arr.length;
document.write(maximumTurns(arr, N));
</script>
|
Time complexity: O(N*log(M)) where M is the maximum value of the array.
Auxiliary Space: O(1)
Last Updated :
12 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...