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

📅  最后修改于: 2021-10-26 02:33:42             🧑  作者: Mango

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

例子:

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

下面是上述方法的实现:

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


Javascript


输出:
4

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程