Open In App

Minimum divide by 2 operations required to make GCD odd for given Array

Last Updated : 31 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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>


 
 

Output: 

1

 

 

Time Complexity: O(N*log N)
Auxiliary Space: O(1)

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads