Open in App
Not now

# Make all the array elements odd with minimum operations of given type

• Difficulty Level : Medium
• Last Updated : 31 Dec, 2022

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:
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:

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 ` `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 s = ``new` `TreeSet(); ` `        ``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

 ``

Output

`4`

Time Complexity: O(n*log(n))
Auxiliary Space: O(n)

My Personal Notes arrow_drop_up
Related Articles