📜  数组上可能的乘以 3 或除以 2 操作的最大次数

📅  最后修改于: 2022-05-13 01:56:07.767000             🧑  作者: Mango

数组上可能的最大乘以 3 或除以 2 操作数

给定一个由N个正整数组成的数组arr[] ,任务是找出每个数组元素可以乘以M或除以K的最大次数。
注意:每次操作中至少有一个元素需要分别除以MK。

例子:

方法:这个问题可以通过观察,连续将一个数组元素除以2 ,偶数元素的计数将在一定数量的步数后减少。因此,为了使匝数最大化,只需将一个偶数元素除以2 ,然后将所有其他元素乘以3一步。请按照以下步骤解决问题:

  • 初始化一个变量,比如Count为 0,它将在数组的每个元素中存储2的幂的计数。
  • 使用变量i[0, N-1]范围内迭代并执行以下步骤:
    • 迭代直到 arr[i]可被2 整除,然后将Count增加1并将arr[i]除以2
  • 执行上述步骤后,打印Count的值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count maximum number
// of multiplication by 3 or division
// by 2 operations that can be performed
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
int 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
// Java program for the above approach
public 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
# Python3 program for the above approach
 
# Function to count maximum number
# of multiplication by 3 or division
# by 2 operations that can be performed
def 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 Input
arr = [ 5, 2, 4 ]
M = 3
K = 2
N = len(arr)
 
# Function Call
print(maximumTurns(arr, N))
 
# This code is contributed by amreshkumar3


C#
// 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 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()
{
 
    // Given Input
    int []arr = { 5, 2, 4 };
    int N = arr.Length;
 
    // Function Call
    Console.Write(maximumTurns(arr, N));
}
}
 
// This code is contributed by ipg2016107.


Javascript


输出:
3

时间复杂度: O(N*log(M)) 其中 M 是数组的最大值。
辅助空间: O(1)