数组上可能的最大乘以 3 或除以 2 操作数
给定一个由N个正整数组成的数组arr[] ,任务是找出每个数组元素可以乘以M或除以K的最大次数。
注意:每次操作中至少有一个元素需要分别除以M和K。
例子:
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
方法:这个问题可以通过观察,连续将一个数组元素除以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)