📜  仅使用数组元素均衡数组

📅  最后修改于: 2022-05-13 01:57:47.895000             🧑  作者: Mango

仅使用数组元素均衡数组

给定一个整数数组,任务是计算最小操作数以使数组相等(使所有数组元素相同)。如果无法均衡,则返回 -1。为了均衡一个数组,我们需要将值从较大的数字移动到较小的数字。操作次数等于运动次数。
例子 :

Input :  arr[] = {1, 3, 2, 0, 4}
Output : 3
We can equalize the array by making value
of all elements equal to 2. To achieve this
we need to do minimum 3 operations (moving
Moving 1 value from arr[1] to arr[0]
Moving 2 values from arr[4] to arr[3]

Input : arr[] = {1, 7, 1}
Output : 4 

方法1(简单):第一种是蛮力方法,我们固定一个元素,然后检查相邻元素,然后借用(或给予)所需的操作量。在这种方法中,我们将需要两个循环,第一个循环用于固定数组的元素,第二个循环用于检查当前元素的其他邻居是否能够在均衡数组中做出贡献.该解的时间复杂度为 O(n 2 );
方法2(高效):
1)求和数组元素。如果 sum % n 不为 0,则返回 -1。
2) 计算平均值或均衡值,如eq = sum/n
3)遍历数组。对于每个元素arr[i]计算eqarr[i]之间差异的绝对值。并跟踪这些差异的总和。让这个总和为diff_sum
4) 返回 diff_sum / 2。

C++
// C++ program to find minimum operations
// needed to equalize an array.
#include 
using namespace std;
 
// Returns minimum operations needed to
// equalize an array.
int minOperations(int arr[], int n)
{
    // Compute sum of array elements
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += arr[i];
 
    // If average of array is not integer,
    // then it is not possible to equalize
    if (sum % n != 0)
        return -1;
 
    // Compute sum of absolute differences
    // between array elements and average
    // or equalized value
    int diff_sum = 0;
    int eq = sum / n;
    for (int i = 0; i < n; i++)
        diff_sum += abs(arr[i] - eq);
 
    return (diff_sum / 2);
}
 
// Driver code
int main()
{
    int arr[] = { 5, 3, 2, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << minOperations(arr, n);
    return 0;
}


Java
// Java program to find minimum operations
// needed to equalize an array.
public class Equalize_Array {
     
    // Returns minimum operations needed to
    // equalize an array.
    static int minOperations(int arr[], int n)
    {
        // Compute sum of array elements
        int sum = 0;
        for (int i = 0; i < n; i++)
            sum += arr[i];
      
        // If average of array is not integer,
        // then it is not possible to equalize
        if (sum % n != 0)
            return -1;
      
        // Compute sum of absolute differences
        // between array elements and average
        // or equalized value
        int diff_sum = 0;
        int eq = sum / n;
        for (int i = 0; i < n; i++)
            diff_sum += Math.abs(arr[i] - eq);
      
        return (diff_sum / 2);
    }
      
    // Driver code
    public static void main(String args[])
    {
        int arr[] = { 5, 3, 2, 6 };
        int n = arr.length;
        System.out.println(minOperations(arr, n));
    }
}
// This code is contributed by Sumit Ghosh


Python3
# Python3 program to find minimum
# operations needed to equalize an array.
 
# Returns minimum operations needed
# to equalize an array.
def minOperations(arr, n):
 
    # Compute sum of array elements
    sum = 0
    for i in range(0,n):
        sum += arr[i]
 
    # If average of array is not integer,
    # then it is not possible to equalize
    if sum % n != 0:
        return -1
 
    # Compute sum of absolute differences
    # between array elements and average
    # or equalized value
    diff_sum = 0
    eq = sum / n
    for i in range(0, n):
        diff_sum += abs(arr[i] - eq)
 
    return int(diff_sum / 2)
 
# Driver code
arr = [5, 3, 2, 6 ]
n = len(arr)
print(minOperations(arr, n))
 
# This code is contributed by Smitha Dinesh Semwal


C#
// C# program to find minimum operations
// needed to equalize an array.
using System;
 
class Equalize_Array {
     
    // Returns minimum operations needed to
    // equalize an array.
    static int minOperations(int []arr, int n)
    {
        // Compute sum of array elements
        int sum = 0;
        for (int i = 0; i < n; i++)
            sum += arr[i];
     
        // If average of array is not integer,
        // then it is not possible to equalize
        if (sum % n != 0)
            return -1;
     
        // Compute sum of absolute differences
        // between array elements and average
        // or equalized value
        int diff_sum = 0;
        int eq = sum / n;
        for (int i = 0; i < n; i++)
            diff_sum += Math.Abs(arr[i] - eq);
     
        return (diff_sum / 2);
    }
     
    // Driver code
    public static void Main()
    {
        int []arr = {5, 3, 2, 6};
        int n = arr.Length;
        Console.WriteLine(minOperations(arr, n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

3

时间复杂度: O(n)
辅助空间: O(1)