📌  相关文章
📜  使所有数组元素相等的最低成本

📅  最后修改于: 2021-04-26 10:37:36             🧑  作者: Mango

给定一个由N个正整数组成的数组arr [] ,任务是在执行以下多次运算(可能为零)之后,以最小的代价使该数组的所有值等于某个整数值。

  1. 将数组元素减少2或以零成本增加2。
  2. 将数组元素减少1或以单位成本增加1。

例子:

方法:基本思想是计算数组中存在的偶数元素和奇数元素的数量,并打印这两者中的最小值作为答案。这种方法之所以有效,是因为我们可以使所有偶数元素相等且所有奇数元素相等而不会产生任何成本(操作1)。执行完这些操作后,更新后的数组将仅包含元素x和x + 1,其中一个为奇数,另一个为偶数。两种类型的元素都可以以1单位成本更改为另一种类型,并且为了使成本最小化,结果将是min(frequency(x),frequency(x +1))。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the minimum cost
// to make each array element equal
int minCost(int arr[], int n)
{
    // To store the count of even numbers
    // present in the array
    int count_even = 0;
  
    // To store the count of odd numbers
    // present in the array
    int count_odd = 0;
  
    // Iterate through the array and
    // find the count of even numbers
    // and odd numbers
    for (int i = 0; i < n; i++) {
        if (arr[i] % 2 == 0)
            count_even++;
        else
            count_odd++;
    }
  
    return min(count_even, count_odd);
}
  
// Driver code
int main()
{
    int arr[] = { 2, 4, 3, 1, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << minCost(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG 
{
      
// Function to return the minimum cost
// to make each array element equal
static int minCost(int arr[], int n)
{
    // To store the count of even numbers
    // present in the array
    int count_even = 0;
  
    // To store the count of odd numbers
    // present in the array
    int count_odd = 0;
  
    // Iterate through the array and
    // find the count of even numbers
    // and odd numbers
    for (int i = 0; i < n; i++) 
    {
        if (arr[i] % 2 == 0)
            count_even++;
        else
            count_odd++;
    }
  
    return Math.min(count_even, count_odd);
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 2, 4, 3, 1, 5 };
    int n = arr.length;
  
    System.out.println(minCost(arr, n));
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
  
# Function to return the minimum cost
# to make each array element equal
def minCost(arr, n):
      
    # To store the count of even numbers
    # present in the array
    count_even = 0
  
    # To store the count of odd numbers
    # present in the array
    count_odd = 0
  
    # Iterate through the array and
    # find the count of even numbers
    # and odd numbers
    for i in range(n):
        if (arr[i] % 2 == 0):
            count_even += 1
        else:
            count_odd += 1
  
    return min(count_even, count_odd)
  
# Driver code
arr = [2, 4, 3, 1, 5]
n = len(arr)
  
print(minCost(arr, n))
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
      
class GFG 
{
      
// Function to return the minimum cost
// to make each array element equal
static int minCost(int []arr, int n)
{
    // To store the count of even numbers
    // present in the array
    int count_even = 0;
  
    // To store the count of odd numbers
    // present in the array
    int count_odd = 0;
  
    // Iterate through the array and
    // find the count of even numbers
    // and odd numbers
    for (int i = 0; i < n; i++) 
    {
        if (arr[i] % 2 == 0)
            count_even++;
        else
            count_odd++;
    }
  
    return Math.Min(count_even, count_odd);
}
  
// Driver code
public static void Main(String[] args)
{
    int []arr = { 2, 4, 3, 1, 5 };
    int n = arr.Length;
  
    Console.WriteLine(minCost(arr, n));
}
}
      
// This code is contributed by Princi Singh


输出:
2

时间复杂度: O(N)
空间复杂度: O(1)