📌  相关文章
📜  用给定类型的最小操作使所有数组元素为奇数

📅  最后修改于: 2021-05-08 16:56:22             🧑  作者: Mango

给定一个由偶数整数组成的数组arr [] 。每次移动时,您都可以从数组中选择任意偶数X并将所有出现的X除以2 。任务是找到所需的最小移动数,以使数组中的所有元素都变为奇数。

例子:

方法:可以使用贪婪方法解决此问题。每次移动时,取数组中最大的剩余偶数并除以2。之所以取最大值,是因为除以2后,它有可能与数组中的其他元素相等,从而使总数最小化。操作。

下面是上述方法的实现:

CPP
// 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 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 s = new SortedSet(); 
        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


输出:
4