 Open in App
Not now

# Minimize division by 2 to make Array of alternate odd and even elements

• Last Updated : 06 Dec, 2022

Given an array arr[] of N positive integers. The task is to find the minimum number of operations required to make the array containing alternating odd and even numbers. In one operation, any array element can be replaced by its half (i.e arr[i]/2).

Examples:

Input: N=6, arr = [4, 10, 6, 6, 2, 7]
Output: 2
Explanation: We can divide elements at index 1 and 3
by 2 to get array [4, 5, 6, 3, 2, 7], which is off alternate parity.

Input: N=6, arr = [3, 10, 7, 18, 9, 66]
Output:  0
Explanation: No operations are needed as array is already alternative.

Approach: To solve the problem use the following idea:

Try both possibilities of the array start with an odd number as well as even number and print the minimum of the two possibilities as the answer.

Follow the steps to solve the problem:

• Declare and Initialize two variables result1 and result2 with 0.
• Iterate the array for all indices from 0 till N – 1.
• If the element at the even index is odd then
• Divide the element by 2 and increment the result1 until it becomes even.
• If the element at the odd index is even then
• Divide the element by 2 and increment the result1 until it becomes odd.
• Iterate the array for all indices from 0 till N – 1.
• If the element at the even index is even then
• Divide the element by 2 and increment the result2 until it becomes odd.
• If the element at the odd index is odd then
• Divide the element by 2 and increment the result2 until it becomes even.
• print minimum of result1 and result2.

Below is the implementation for the above approach:

## C++

```// C++ code to implement the approach

#include <bits/stdc++.h>
using namespace std;

// Function to find the minimum number of operations
int minOperations(int arr[], int n)
{
// Two variables to count number of operations
int result1 = 0, result2 = 0;

// For array starting with even element
for (int i = 0; i < n; i++) {
int element = arr[i];

// For even indices
if (i % 2 == 0) {

// If element is already even
if (element % 2 == 0)
continue;

// Otherwise keep dividing by 2
// till element becomes even
else {
while (element % 2 == 1) {
element /= 2;
result1++;
}
}
}

// For odd indices
else {

// If element is already odd
if (element % 2 == 1)
continue;

// Otherwise keep dividing by 2
// till element becomes odd
else {
while (element % 2 == 0) {
element /= 2;
result1++;
}
}
}
}

// For array starting from odd element
for (int i = 0; i < n; i++) {
int element = arr[i];

// For even indices
if (i % 2 == 0) {

// If element is already odd
if (element % 2 == 1)
continue;

// Otherwise keep dividing by 2
// till element becomes odd
else {
while (element % 2 == 0) {
element /= 2;
result2++;
}
}
}

// For odd indices
else {

// If element is already even
if (element % 2 == 0)
continue;

// Otherwise keep dividing by 2
// till element becomes even
else {
while (element % 2 == 1) {
element /= 2;
result2++;
}
}
}
}
return min(result1, result2);
}

// Driver code
int main()
{
int N = 6;
int arr[] = { 4, 10, 6, 6, 2, 3 };

// Function call
cout << minOperations(arr, N);
return 0;
}```

## Java

```// Java code to implement the approach
import java.io.*;

class GFG
{

// Function to find the minimum number of operations
public static int minOperations(int arr[], int n)
{
// Two variables to count number of operations
int result1 = 0, result2 = 0;

// For array starting with even element
for (int i = 0; i < n; i++) {
int element = arr[i];

// For even indices
if (i % 2 == 0) {

// If element is already even
if (element % 2 == 0)
continue;

// Otherwise keep dividing by 2
// till element becomes even
else {
while (element % 2 == 1) {
element /= 2;
result1++;
}
}
}

// For odd indices
else {

// If element is already odd
if (element % 2 == 1)
continue;

// Otherwise keep dividing by 2
// till element becomes odd
else {
while (element % 2 == 0) {
element /= 2;
result1++;
}
}
}
}

// For array starting from odd element
for (int i = 0; i < n; i++) {
int element = arr[i];

// For even indices
if (i % 2 == 0) {

// If element is already odd
if (element % 2 == 1)
continue;

// Otherwise keep dividing by 2
// till element becomes odd
else {
while (element % 2 == 0) {
element /= 2;
result2++;
}
}
}

// For odd indices
else {

// If element is already even
if (element % 2 == 0)
continue;

// Otherwise keep dividing by 2
// till element becomes even
else {
while (element % 2 == 1) {
element /= 2;
result2++;
}
}
}
}
return Math.min(result1, result2);
}

// Driver Code
public static void main(String[] args)
{
int N = 6;
int arr[] = { 4, 10, 6, 6, 2, 3 };

// Function call
System.out.print(minOperations(arr, N));
}
}

// This code is contributed by Rohit Pradhan```

## Python3

```# python3 code to implement the approach

# Function to find the minimum number of operations
def minOperations(arr, n):

# Two variables to count number of operations
result1 = 0
result2 = 0

# For array starting with even element
for i in range(0, n):
element = arr[i]

# For even indices
if (i % 2 == 0):

# If element is already even
if (element % 2 == 0):
continue

# Otherwise keep dividing by 2
# till element becomes even
else:
while (element % 2 == 1):
element //= 2
result1 += 1

# For odd indices
else:

# If element is already odd
if (element % 2 == 1):
continue

# Otherwise keep dividing by 2
# till element becomes odd
else:
while (element % 2 == 0):
element //= 2
result1 += 1

# For array starting from odd element
for i in range(0, n):
element = arr[i]

# For even indices
if (i % 2 == 0):

# If element is already odd
if (element % 2 == 1):
continue

# Otherwise keep dividing by 2
# till element becomes odd
else:
while (element % 2 == 0):
element //= 2
result2 += 1

# For odd indices
else:

# If element is already even
if (element % 2 == 0):
continue

# Otherwise keep dividing by 2
# till element becomes even
else:
while (element % 2 == 1):
element //= 2
result2 += 1

return min(result1, result2)

# Driver code
if __name__ == "__main__":

N = 6
arr = [4, 10, 6, 6, 2, 3]

# Function call
print(minOperations(arr, N))

# This code is contributed by rakeshsahni
```

## C#

```// C# code to implement the approach
using System;
class GFG {

// Function to find the minimum number of operations
public static int minOperations(int[] arr, int n)
{

// Two variables to count number of operations
int result1 = 0, result2 = 0;

// For array starting with even element
for (int i = 0; i < n; i++) {
int element = arr[i];

// For even indices
if (i % 2 == 0) {

// If element is already even
if (element % 2 == 0)
continue;

// Otherwise keep dividing by 2
// till element becomes even
else {
while (element % 2 == 1) {
element /= 2;
result1++;
}
}
}

// For odd indices
else {

// If element is already odd
if (element % 2 == 1)
continue;

// Otherwise keep dividing by 2
// till element becomes odd
else {
while (element % 2 == 0) {
element /= 2;
result1++;
}
}
}
}

// For array starting from odd element
for (int i = 0; i < n; i++) {
int element = arr[i];

// For even indices
if (i % 2 == 0) {

// If element is already odd
if (element % 2 == 1)
continue;

// Otherwise keep dividing by 2
// till element becomes odd
else {
while (element % 2 == 0) {
element /= 2;
result2++;
}
}
}

// For odd indices
else {

// If element is already even
if (element % 2 == 0)
continue;

// Otherwise keep dividing by 2
// till element becomes even
else {
while (element % 2 == 1) {
element /= 2;
result2++;
}
}
}
}
return Math.Min(result1, result2);
}

// Driver code
public static void Main(string[] args)
{
int N = 6;
int[] arr = { 4, 10, 6, 6, 2, 3 };

// Function call
Console.Write(minOperations(arr, N));
}
}

// This code is contributed by code_hunt.```

## Javascript

```<script>
// JS code to implement the approach

// Function to find the minimum number of operations
function minOperations(arr, n) {
// Two variables to count number of operations
let result1 = 0, result2 = 0;

// For array starting with even element
for (let i = 0; i < n; i++) {
let element = arr[i];

// For even indices
if (i % 2 == 0) {

// If element is already even
if (element % 2 == 0)
continue;

// Otherwise keep dividing by 2
// till element becomes even
else {
while (element % 2 == 1) {
element = Math.floor(element / 2);
result1++;
}
}
}

// For odd indices
else {

// If element is already odd
if (element % 2 == 1)
continue;

// Otherwise keep dividing by 2
// till element becomes odd
else {
while (element % 2 == 0) {
element = Math.floor(element / 2);
result1++;
}
}
}
}

// For array starting from odd element
for (let i = 0; i < n; i++) {
let element = arr[i];

// For even indices
if (i % 2 == 0) {

// If element is already odd
if (element % 2 == 1)
continue;

// Otherwise keep dividing by 2
// till element becomes odd
else {
while (element % 2 == 0) {
element = Math.floor(element / 2);
result2++;
}
}
}

// For odd indices
else {

// If element is already even
if (element % 2 == 0)
continue;

// Otherwise keep dividing by 2
// till element becomes even
else {
while (element % 2 == 1) {
element = Math.floor(element / 2);
result2++;
}
}
}
}
return Math.min(result1, result2);
}

// Driver code

let N = 6;
let arr = [4, 10, 6, 6, 2, 3];

// Function call
document.write(minOperations(arr, N));

</script>```

## PHP

```<?php

// Function to find the minimum number of operations
function minOperations(\$arr, \$n)
{
// Two variables to count number of operations
\$result1 = 0;
\$result2 = 0;

// For array starting with even element
for (\$i = 0; \$i < \$n; \$i++) {
\$element = \$arr[\$i];

// For even indices
if (\$i % 2 == 0) {

// If element is already even
if (\$element % 2 == 0)
continue;

// Otherwise keep dividing by 2
// till element becomes even
else {
while (\$element % 2 == 1) {
\$element /= 2;
\$result1++;
}
}
}

// For odd indices
else {

// If element is already odd
if (\$element % 2 == 1)
continue;

// Otherwise keep dividing by 2
// till element becomes odd
else {
while (\$element % 2 == 0) {
\$element /= 2;
\$result1++;
}
}
}
}

// For array starting from odd element
for (\$i = 0; \$i < \$n; \$i++) {
\$element = \$arr[\$i];

// For even indices
if (\$i % 2 == 0) {

// If element is already odd
if (\$element % 2 == 1)
continue;

// Otherwise keep dividing by 2
// till element becomes odd
else {
while (\$element % 2 == 0) {
\$element /= 2;
\$result2++;
}
}
}

// For odd indices
else {

// If element is already even
if (\$element % 2 == 0)
continue;

// Otherwise keep dividing by 2
// till element becomes even
else {
while (\$element % 2 == 1) {
\$element /= 2;
\$result2++;
}
}
}
}
return min(\$result1, \$result2);
}

// Driver code
\$N = 6;
\$arr = array(4, 10, 6, 6, 2, 3);

// Function call
echo minOperations(\$arr, \$N);
// This code is contributed by Kanishka Gupta
?>```
Output

`2`

Time Complexity: O(N * log(max(arr[i]))),  where max(arr[i]) is maximum element in array.
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up