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++
#include <bits/stdc++.h>
using namespace std;
int minimumOperations( int arr[], int N)
{
int mini = INT_MAX;
for ( int i = 0; i < N; i++) {
int count = 0;
while (arr[i] % 2 == 0) {
arr[i] = arr[i] / 2;
count++;
}
if (mini > count) {
mini = count;
}
}
return mini;
}
int main()
{
int arr[] = { 4, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << minimumOperations(arr, N);
return 0;
}
|
Java
class GFG{
public static int minimumOperations( int arr[], int N)
{
int mini = Integer.MAX_VALUE;
for ( int i = 0 ; i < N; i++) {
int count = 0 ;
while (arr[i] % 2 == 0 ) {
arr[i] = arr[i] / 2 ;
count++;
}
if (mini > count) {
mini = count;
}
}
return mini;
}
public static void main(String args[])
{
int arr[] = { 4 , 6 };
int N = arr.length;
System.out.println(minimumOperations(arr, N));
}
}
|
Python3
INT_MAX = 2147483647
def minimumOperations(arr, N):
mini = INT_MAX
for i in range ( 0 , N):
count = 0
while (arr[i] % 2 = = 0 ):
arr[i] = arr[i] / / 2
count + = 1
if (mini > count):
mini = count
return mini
if __name__ = = "__main__" :
arr = [ 4 , 6 ]
N = len (arr)
print (minimumOperations(arr, N))
|
C#
using System;
class GFG {
public static int minimumOperations( int [] arr, int N)
{
int mini = Int32.MaxValue;
for ( int i = 0; i < N; i++) {
int count = 0;
while (arr[i] % 2 == 0) {
arr[i] = arr[i] / 2;
count++;
}
if (mini > count) {
mini = count;
}
}
return mini;
}
public static void Main( string [] args)
{
int [] arr = { 4, 6 };
int N = arr.Length;
Console.WriteLine(minimumOperations(arr, N));
}
}
|
Javascript
<script>
function minimumOperations(arr, N)
{
let mini = Number.MAX_SAFE_INTEGER;
for (let i = 0; i < N; i++)
{
let count = 0;
while (arr[i] % 2 == 0) {
arr[i] = Math.floor(arr[i] / 2);
count++;
}
if (mini > count) {
mini = count;
}
}
return mini;
}
let arr = [4, 6];
let N = arr.length;
document.write(minimumOperations(arr, N));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...