使给定数组的 GCD 奇数所需的最小除以 2 操作
给定一个包含N个正整数的数组arr[] ,任务是找到使数组元素的 GCD 为奇数所需的最小操作数,使得在每个操作中,一个数组元素可以除以2 。
例子:
Input: arr[] = {4, 6}
Output: 1
Explanation:
Below are the operations performed:
Operation 1: Divide the array element arr[0](= 4) by 2 modifies the array to {2, 6}.
Operation 2: Divide the array element arr[0](= 2) by 2 modifies the array to {1, 6}.
After the above operations, the GCD of the array elements is 1 which is odd. Therefore, the minimum number of operations required is 2.
Input: arr[] = {2, 4, 1}
Output: 0
方法:给定的问题可以通过找到每个数组元素的 2 的幂的计数来解决给定的问题,并且 2 的最小幂(比如C )将给出最少的操作,因为在将该元素除以2 C之后,元素变为奇数,这导致数组的 GCD 为奇数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum number
// of operations to make the GCD of
// the array odd
int minimumOperations(int arr[], int N)
{
// Stores the minimum operations
// required
int mini = INT_MAX;
for (int i = 0; i < N; i++) {
// Stores the powers of two for
// the current array element
int count = 0;
// Dividing by 2
while (arr[i] % 2 == 0) {
arr[i] = arr[i] / 2;
// Increment the count
count++;
}
// Update the minimum operation
// required
if (mini > count) {
mini = count;
}
}
// Return the result required
return mini;
}
// Driver Code
int main()
{
int arr[] = { 4, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << minimumOperations(arr, N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to find the minimum number
// of operations to make the GCD of
// the array odd
public static int minimumOperations(int arr[], int N)
{
// Stores the minimum operations
// required
int mini = Integer.MAX_VALUE;
for (int i = 0; i < N; i++) {
// Stores the powers of two for
// the current array element
int count = 0;
// Dividing by 2
while (arr[i] % 2 == 0) {
arr[i] = arr[i] / 2;
// Increment the count
count++;
}
// Update the minimum operation
// required
if (mini > count) {
mini = count;
}
}
// Return the result required
return mini;
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 4, 6 };
int N = arr.length;
System.out.println(minimumOperations(arr, N));
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# python program for the above approach
INT_MAX = 2147483647
# Function to find the minimum number
# of operations to make the GCD of
# the array odd
def minimumOperations(arr, N):
# Stores the minimum operations
# required
mini = INT_MAX
for i in range(0, N):
# Stores the powers of two for
# the current array element
count = 0
# Dividing by 2
while (arr[i] % 2 == 0):
arr[i] = arr[i] // 2
# Increment the count
count += 1
# Update the minimum operation
# required
if (mini > count):
mini = count
# Return the result required
return mini
# Driver Code
if __name__ == "__main__":
arr = [4, 6]
N = len(arr)
print(minimumOperations(arr, N))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the minimum number
// of operations to make the GCD of
// the array odd
public static int minimumOperations(int[] arr, int N)
{
// Stores the minimum operations
// required
int mini = Int32.MaxValue;
for (int i = 0; i < N; i++) {
// Stores the powers of two for
// the current array element
int count = 0;
// Dividing by 2
while (arr[i] % 2 == 0) {
arr[i] = arr[i] / 2;
// Increment the count
count++;
}
// Update the minimum operation
// required
if (mini > count) {
mini = count;
}
}
// Return the result required
return mini;
}
// Driver Code
public static void Main(string[] args)
{
int[] arr = { 4, 6 };
int N = arr.Length;
Console.WriteLine(minimumOperations(arr, N));
}
}
// This code is contributed by ukasp.
Javascript
输出:
1
时间复杂度: O(N*log N)
辅助空间: O(1)