Minimum divide by 2 operations required to make GCD odd for given Array
Last Updated :
31 Jan, 2022
Given an array arr[] of N positive integers, the task is to find the minimum number of operations required to make the GCD of array element odd such that in each operation an array element can be divided by 2.
Examples:
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
Approach: The given problem can be solved based on the observation by finding the count of powers of 2 for each array element and the minimum power of 2(say C) will give the minimum operations because after dividing that element by 2C the element becomes odd and that results in the GCD of the array as odd.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
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
<script>
// Javascript program for the above approach
// Function to find the minimum number
// of operations to make the GCD of
// the array odd
function minimumOperations(arr, N)
{
// Stores the minimum operations
// required
let mini = Number.MAX_SAFE_INTEGER;
for (let i = 0; i < N; i++)
{
// Stores the powers of two for
// the current array element
let count = 0;
// Dividing by 2
while (arr[i] % 2 == 0) {
arr[i] = Math.floor(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
let arr = [4, 6];
let N = arr.length;
document.write(minimumOperations(arr, N));
// This code is contributed by saurabh_jaiswal.
</script>
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Similar Reads
Minimum operations required to make all elements in an array of first N odd numbers equal Given an array consisting of first N odd numbers, the task is to find the minimum number of operations required to make all the array elements equal by repeatedly selecting a pair and incrementing one element and decrementing the other element in the pair by 1. Examples: Input: N = 3Output: 2Explana
10 min read
Minimum gcd operations to make all array elements one Given an array A[] of size N. You can replace any number in the array with the gcd of that element with any of its adjacent elements. Find the minimum number of such operation to make the element of whole array equal to 1. If its not possible print -1. Examples: Input : A[] = {4, 8, 9} Output : 3 Ex
8 min read
Minimize deviation of an array by given operations Given an array A[] consisting of positive integers, the task is to calculate the minimum possible deviation of the given arrayA[] after performing the following operations any number of times: Operation 1: If the array element is even, divide it by 2.Operation 2: If the array element is odd, multipl
7 min read
Minimum operations to make GCD of array a multiple of k Given an array and k, we need to find the minimum operations needed to make GCD of the array equal or multiple of k. Here an operation means either increment or decrements an array element by 1. Examples: Input : a = { 4, 5, 6 }, k = 5 Output : 2 Explanation : We can increase 4 by 1 so that it becom
8 min read
Minimum deletions required to make GCD of the array equal to 1 Given an array arr[] of N integers, the task is to find the minimum deletions required to make the GCD of the resulting array elements equal to 1. If it is impossible then print -1.Examples: Input: arr[] = {2, 4, 6, 3} Output: 0 It is clear that GCD(2, 4, 6, 3) = 1 So, we do not need to delete any e
5 min read
Minimize increments required to make differences between all pairs of array elements even Given an array, arr[] consisting of N integers, the task is to minimize the number of increments of array elements required to make all differences pairs of array elements even. Examples: Input: arr[] = {4, 1, 2}Output: 1Explanation: Operation 1: Increment arr[1] by 1. The array arr[] modifies to {4
5 min read