📌  相关文章
📜  通过将数组元素减少到最小次数为一半,使所有数组元素相等

📅  最后修改于: 2021-04-17 14:07:43             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是通过在每个操作中将A i转换为A i / 2来最小化使所有数组元素相等所需的操作数。

例子:

方法:解决此问题的想法是使用贪婪方法。步骤如下:

  • 初始化辅助地图,例如mp。
  • 遍历数组,并为每个数组元素将元素除以2,直到将其减少为1,然后将结果数存储在Map中
  • 遍历地图,找到频率等于N的最大元素,例如mx
  • 再次遍历数组,并为每个元素将元素除以2,直到等于mx并递增计数。
  • 打印计数为所需的最少操作数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find minimum number of operations
int minOperations(int arr[], int N)
{
    // Initialize map
    map mp;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
        int res = arr[i];
 
        // Divide current array
        // element until it reduces to 1
        while (res) {
 
            mp[res]++;
            res /= 2;
        }
    }
 
    int mx = 1;
    // Traverse the map
    for (auto it : mp) {
        // Find the maximum element
        // having frequency equal to N
        if (it.second == N) {
            mx = it.first;
        }
    }
 
    // Stores the minimum number
    // of operations required
    int ans = 0;
 
    for (int i = 0; i < N; i++) {
        int res = arr[i];
 
        // Count operations required to
        // convert current element to mx
        while (res != mx) {
 
            ans++;
 
            res /= 2;
        }
    }
 
    // Print the answer
    cout << ans;
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 3, 1, 1, 3 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    minOperations(arr, N);
}


Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
  // Function to find minimum number of operations
  static void minOperations(int[] arr, int N)
  {
    // Initialize map
    HashMap mp = new HashMap();
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
      int res = arr[i];
 
      // Divide current array
      // element until it reduces to 1
      if (mp.containsKey(res))
        mp.put(res, mp.get(res) + 1);
      else
        mp.put(res, 1);
 
      res /= 2;
    }   
    int mx = 1;
 
    for(Map.Entry it : mp.entrySet())
    {
 
      // Find the maximum element
      // having frequency equal to N
      if (it.getValue() == N)
      {
        mx = it.getKey();
      }
    }
 
    // Stores the minimum number
    // of operations required
    int ans = 0;
 
    for (int i = 0; i < N; i++)
    {
      int res = arr[i];
 
      // Count operations required to
      // convert current element to mx
      while (res != mx)
      {
        ans++;
        res /= 2;
      }
    }
 
    // Print the answer
    System.out.println(ans);
  }      
 
  // Driver Code
  public static void main(String[] args)
  {
    // Given array
    int arr[] = { 3, 1, 1, 3 };
 
    // Size of the array
    int N = arr.length;
    minOperations(arr, N);
  }
}
 
// This code is contributed by code_hunt.


Python3
# Python program for the above approach
# Function to find minimum number of operations
def minOperations(arr, N):
   
  # Initialize map
  mp = {}
   
  # Traverse the array
  for i in range(N):
    res = arr[i]
     
    # Divide current array
    # element until it reduces to 1
    while (res):
      if res in mp:
        mp[res] += 1
      else:
        mp[res] = 1
      res //= 2
  mx = 1
   
  # Traverse the map
  for it in mp:
     
    # Find the maximum element
    # having frequency equal to N
    if (mp[it] == N):
      mx = it
       
  # Stores the minimum number
  # of operations required
  ans = 0
  for i in range(N):
    res = arr[i]
     
    # Count operations required to
    # convert current element to mx
    while (res != mx):
      ans += 1
      res //= 2
       
  # Print the answer
  print(ans)
 
# Driver Code
# Given array
arr = [ 3, 1, 1, 3 ]
 
# Size of the array
N = len(arr)
minOperations(arr, N)
 
# This code is contributed by rohitsingh07052.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
     
    // Function to find minimum number of operations
    static void minOperations(int[] arr, int N)
    {
        // Initialize map
        Dictionary mp = new Dictionary();
      
        // Traverse the array
        for (int i = 0; i < N; i++) {
            int res = arr[i];
      
            // Divide current array
            // element until it reduces to 1
            while (res > 0) {
                if(mp.ContainsKey(res))
                {
                    mp[res]++;
                }
                else{
                    mp[res] = 1;
                }
                res /= 2;
            }
        }   
        int mx = 1;
        
        foreach(KeyValuePair it in mp)
        {
           
            // Find the maximum element
            // having frequency equal to N
            if (it.Value == N)
            {
                mx = it.Key;
            }
        }
      
        // Stores the minimum number
        // of operations required
        int ans = 0;
      
        for (int i = 0; i < N; i++)
        {
            int res = arr[i];
      
            // Count operations required to
            // convert current element to mx
            while (res != mx)
            {
                ans++;
                res /= 2;
            }
        }
      
        // Print the answer
        Console.Write(ans);
    }
 
  // Driver code
  static void Main()
  {
     
    // Given array
    int[] arr = { 3, 1, 1, 3 };
  
    // Size of the array
    int N = arr.Length;
  
    minOperations(arr, N);
  }
}
 
// This code is contributed by divyesh072019.


输出:
2

时间复杂度: O(N * log(max(arr [i]))
辅助空间: O(N)