Make all the array elements odd with minimum operations of given type
Last Updated :
12 Jul, 2025
Given an array arr[] consisting of even integers. At each move, you can select any even number X from the array and divide all the occurrences of X by 2. The task is to find the minimum number of moves needed so that all the elements in the array become odd.
Examples:
Input: arr[] = {40, 6, 40, 20}
Output: 4
Move 1: Select 40 and divide all the occurrences
of 40 by 2 to get {20, 6, 20, 20}
Move 2: Select 20 and divide all the occurrences
of 20 by 2 to get {10, 6, 10, 10}
Move 3: Select 10 and divide all the occurrences
of 10 by 2 to get {5, 6, 5, 5}.
Move 4: Select 6 and divide it by 2 to get {5, 3, 5, 5}.
Input: arr[] = {2, 4, 16, 8}
Output: 4
Approach: This problem can be solved using greedy approach. At every move, take the largest remaining even number in the array and divide it by 2. The largest is taken because there is a chance that it can become equal to some other element in the array after it is divided by 2 which minimizes the total operations.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the count of
// minimum operations required
int minOperations(int arr[], int n)
{
// Insert all the elements in a set
set<int> s;
for (int i = 0; i < n; i++) {
s.insert(arr[i]);
}
// To store the number of moves
int moves = 0;
// While the set is not empty
while (s.empty() == 0) {
// The last element of the set
int z = *(s.rbegin());
// If the number is even
if (z % 2 == 0) {
moves++;
s.insert(z / 2);
}
// Remove the element from the set
s.erase(z);
}
return moves;
}
// Driver code
int main()
{
int arr[] = { 40, 6, 40, 20 };
int n = sizeof(arr) / sizeof(int);
cout << minOperations(arr, n);
return 0;
}
Java
// Java implementation of the approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to return the count of
// minimum operations required
static int minOperations(int arr[], int n)
{
// Insert all the elements in a set
TreeSet<Integer> s = new TreeSet<Integer>();
for (int i = 0; i < n; i++)
{
s.add(arr[i]);
}
// To store the number of moves
int moves = 0;
// While the set is not empty
while (s.size() != 0)
{
// The last element of the set
Integer z = s.last();
// If the number is even
if (z % 2 == 0)
{
moves++;
s.add(z / 2);
}
// Remove the element from the set
s.remove(z);
}
return moves;
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 40, 6, 40, 20 };
int n = arr.length;
System.out.println(minOperations(arr, n));
}
}
// This code is contributed by ApurvaRaj
Python3
# Python3 implementation of the approach
from collections import OrderedDict as mpp
# Function to return the count of
# minimum operations required
def minOperations(arr, n):
# Insert all the elements in a set
s = mpp()
for i in range(n):
s[arr[i]] = 1
# To store the number of moves
moves = 0
# While the set is not empty
while (len(s) > 0):
# The last element of the set
z = sorted(list(s.keys()))[-1]
# If the number is even
if (z % 2 == 0):
moves += 1
s[z / 2] = 1
# Remove the element from the set
del s[z]
return moves
# Driver code
arr = [40, 6, 40, 20]
n = len(arr)
print(minOperations(arr, n))
# This code is contributed by mohit kumar 29
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to return the count of
// minimum operations required
static int minOperations(int []arr, int n)
{
// Insert all the elements in a set
SortedSet<int> s = new SortedSet<int>();
for (int i = 0; i < n; i++)
{
s.Add(arr[i]);
}
// To store the number of moves
int moves = 0;
// While the set is not empty
while (s.Count != 0)
{
// The last element of the set
int z = s.Max;
// If the number is even
if (z % 2 == 0)
{
moves++;
s.Add(z / 2);
}
// Remove the element from the set
s.Remove(z);
}
return moves;
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 40, 6, 40, 20 };
int n = arr.Length;
Console.WriteLine(minOperations(arr, n));
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript implementation of the approach
// Function to find all the distinct
// remainders when n is divided by
// all the elements from
// the range [1, n + 1]
function findRemainders(n)
{
// Set will be used to store
// the remainders in order
// to eliminate duplicates
var vc = new Set();
// Find the remainders
for(var i = 1; i <= Math.ceil(Math.sqrt(n)); i++)
vc.add(parseInt(n / i));
for(var i = parseInt(n / Math.ceil(Math.sqrt(n))) - 1;
i >= 0; i--)
vc.add(i);
// Print the contents of the set
[...vc].sort((a, b) => a - b).forEach(it => {
document.write(it + " ");
});
}
// Driver code
var n = 5;
findRemainders(n);
// This code is contributed by famously
</script>
Time Complexity: O(n*log(n))
Auxiliary Space: O(n)
Similar Reads
Make all the elements of array even with given operations Given an array arr[] of positive integers, find the minimum number of operations required to make all the array elements even where: If there is an odd number, then, increment the element and the next adjacent element by 1.Each increment costs one operation. Note: If there is any number in arr[] whi
6 min read
Make all the elements of array even with given operations Given an array arr[] of positive integers, find the minimum number of operations required to make all the array elements even where: If there is an odd number, then, increment the element and the next adjacent element by 1.Each increment costs one operation. Note: If there is any number in arr[] whi
6 min read
Minimum operation to make all elements equal in array Given an array consisting of n positive integers, the task is to find the minimum number of operations to make all elements equal. In each operation, we can perform addition, multiplication, subtraction, or division with any number and an array element. Examples: Input : arr[] = [1, 2, 3, 4]Output :
11 min read
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
Make all the elements of equal by using given operation Given an array A[] of length N, the task is to return the minimum number of times the below-given operations are required to perform so that all elements become equal. If it is not possible then return -1. Choose two distinct indices i and j. Such that A[i] > A[j]Let X = Ceil((A[i] - A[j])/2)Subt
8 min read
Reduce the array to a single element with the given operation Given an integer N and an array arr containing integers from 1 to N in a sorted fashion. The task is to reduce the array to a single element by performing the following operation: All the elements in the odd positions will be removed after a single operation. This operation will be performed until o
4 min read