📌  相关文章
📜  使数组元素等于最小成本

📅  最后修改于: 2021-05-17 19:41:53             🧑  作者: Mango

给定整数数组arr [] ,任务是通过执行以下两个操作来找到使数组元素相等的最小步骤数–

  • 0的成本从元素中减去2
  • 1成本从元素中添加或减去1

例子:

方法:这个想法是利用将2加到数组的任何元素上将花费0的事实。另外,第二个观察结果是,将2加到任何奇数上都会得到一个奇数,而类似地将2加到一个偶数上也会得到这个结果。仅以偶数为单位。因此,任何元素都可以以零成本等于最接近的整数,但具有相同的属性,即如果它的初始值是奇数,则它将保持为奇数,或者即使它的初始值是偶数,也将保留为零。因此,要找到最小成本,我们可以找到数组中奇数或偶数元素的最小计数。
算法:

  • 查找数组中奇数或偶数元素的计数(例如count )。
  • 在count和(length – count)之间找到最小值,其中length是数组的长度。
  • 最小值将是使所有数组元素均等的期望成本。

举例说明:

Given Array be  - {4, 2, 3, 1}
Count of Odd Elements - 2 ({3, 1})
Count of Even Elements - 2 ({4, 2})

Hence, the minimum cost required is 2 and
the array elements can be made equal 
to any integer of even or odd value

下面是上述方法的实现:

C++
// C++ implementation to make
// array elements equal with
// minimum cost
 
#include 
using namespace std;
 
// Function to find the minimum
// cost required to make array
// elements equal
void makearrayequal(int arr[], int n)
{
    int x = 0;
     
    // Loop to find the
    // count of odd elements
    for (int i = 0; i < n; i++) {
        x += arr[i] & 1;
    }
 
    cout << min(x, n - x) << endl;
}
 
// Driver Code
int main()
{
    int arr[] = { 4, 3, 2, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    makearrayequal(arr, n);
    return 0;
}


Java
// Java implementation to make
// array elements equal with
// minimum cost
class GFG {
 
    // Function to find the minimum
    // cost required to make array
    // elements equal
    static void makearrayequal(int arr[], int n)
    {
        int x = 0;
         
        // Loop to find the
        // count of odd elements
        for (int i = 0; i < n; i++) {
            x += (arr[i] & 1);
        }
     
        System.out.println(Math.min(x, n - x));
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int arr[] = { 4, 3, 2, 1 };
        int n = arr.length;
        makearrayequal(arr, n);
        
    }
}
 
// This code is contributed by Yash_R


Python3
# Python3 implementation to make
# array elements equal with
# minimum cost
 
# Function to find the minimum
# cost required to make array
# elements equal
def makearrayequal(arr,  n) :
    x = 0;
     
    # Loop to find the
    # count of odd elements
    for i in range(n) :
        x += arr[i] & 1;
 
    print(min(x, n - x));
 
# Driver Code
if __name__ == "__main__" :
 
    arr = [ 4, 3, 2, 1 ];
    n = len(arr);
    makearrayequal(arr, n);
   
# This code is contributed by Yash_R


C#
// C# implementation to make
// array elements equal with
// minimum cost
using System;
 
class GFG {
 
    // Function to find the minimum
    // cost required to make array
    // elements equal
    static void makearrayequal(int []arr, int n)
    {
        int x = 0;
         
        // Loop to find the
        // count of odd elements
        for (int i = 0; i < n; i++) {
            x += (arr[i] & 1);
        }
     
        Console.WriteLine(Math.Min(x, n - x));
    }
     
    // Driver Code
    public static void Main (string[] args)
    {
        int []arr = { 4, 3, 2, 1 };
        int n = arr.Length;
        makearrayequal(arr, n);
        
    }
}
 
// This code is contributed by Yash_R


Javascript


输出:
2

性能分析:

  • 时间复杂度: O(N)。
  • 辅助空间: O(1)