📌  相关文章
📜  通过最小增量减量操作最小化和最大数组元素之间的绝对差

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

通过最小增量减量操作最小化和最大数组元素之间的绝对差

给定一个由N个正整数组成的数组arr[] ,任务是最小化所需的操作数,以最小化数组中存在的最小元素和最大元素之间的绝对差。在每个操作中,从一个数组元素中减去1 ,然后将1增加到另一个数组元素。

例子:

方法:给定的问题可以通过观察数组元素的递增和递减1成对执行的事实来解决,因此如果数组元素的总和可被N整除,则所有数组元素都可以求和/N 。否则,某些元素将具有值sum/N ,而某些元素在执行给定操作后将具有值(sum/N + 1) 。请按照以下步骤解决给定的问题:

  • 初始化一个辅助数组,比如final[] ,它存储具有所需最小差的结果数组。
  • 按升序对给定数组进行排序。
  • 遍历给定数组,如果当前索引i小于sum%N ,则将最终数组的当前元素更新为值(sum/N + 1) 。否则,将final[i]更新为(sum/N)
  • 反转数组final[]
  • 初始化一个变量,比如ans = 0 ,它存储将arr[]转换为final[]的最小操作数。
  • 同时遍历数组arr[]final[] ,并将arr[i]final[i]之差的绝对值添加到变量ans中。
  • 完成上述步骤后,打印ans/2的值作为结果最小操作。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to minimize the operations
// for the difference between minimum
// and maximum element by incrementing
// decrementing array elements in pairs
void countMinimumSteps(int arr[], int N)
{
    // Stores the sum of the array
    int sum = 0;
 
    // Find the sum of array element
    for (int i = 0; i < N; i++) {
        sum += arr[i];
    }
 
    // Stores the resultant final array
    int finalArray[N];
 
    // Iterate over the range [0, N]
    for (int i = 0; i < N; ++i) {
 
        // Assign values to finalArray
        if (i < sum % N) {
            finalArray[i] = sum / N + 1;
        }
        else {
            finalArray[i] = sum / N;
        }
    }
 
    // Reverse the final array
    reverse(finalArray, finalArray + N);
 
    // Stores the minimum number of
    // operations required
    int ans = 0;
 
    // Update the value of ans
    for (int i = 0; i < N; ++i) {
        ans += abs(arr[i] - finalArray[i]);
    }
 
    // Print the result
    cout << ans / 2;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
    countMinimumSteps(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
static void reverse(int a[], int n)
{
    int i, k, t;
    for(i = 0; i < n / 2; i++)
    {
        t = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = t;
    }
}
 
// Function to minimize the operations
// for the difference between minimum
// and maximum element by incrementing
// decrementing array elements in pairs
static void countMinimumSteps(int arr[], int N)
{
     
    // Stores the sum of the array
    int sum = 0;
 
    // Find the sum of array element
    for(int i = 0; i < N; i++)
    {
        sum += arr[i];
    }
 
    // Stores the resultant final array
    int finalArray[] = new int[N];
 
    // Iterate over the range [0, N]
    for(int i = 0; i < N; ++i)
    {
         
        // Assign values to finalArray
        if (i < sum % N)
        {
            finalArray[i] = sum / N + 1;
        }
        else
        {
            finalArray[i] = sum / N;
        }
    }
 
    // Reverse the final array
    reverse(finalArray, finalArray.length);
 
    // Stores the minimum number of
    // operations required
    int ans = 0;
 
    // Update the value of ans
    for(int i = 0; i < N; ++i)
    {
        ans += Math.abs(arr[i] - finalArray[i]);
    }
 
    // Print the result
    System.out.println(ans / 2);
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 6 };
    int N = arr.length;
     
    countMinimumSteps(arr, N);
}
}
 
// This code is contributed by abhinavjain194


Python3
# Python program for the above approach
 
# Function to minimize the operations
# for the difference between minimum
# and maximum element by incrementing
# decrementing array elements in pairs
def countMinimumSteps(arr, N):
 
    # Stores the sum of the array
    sum = 0;
 
    # Find the sum of array element
    for i in range(N):
        sum += arr[i];
 
    # Stores the resultant final array
    finalArray = [0] * N;
 
    # Iterate over the range [0, N]
 
    for i in range(0, N):
        #print(i)
 
        # Assign values to finalArray
        if (i < sum % N):
            finalArray[i] = (sum // N)+ 1;
        else:
            finalArray[i] = sum // N;
 
    # Reverse the final array
    finalArray = finalArray[::-1];
 
    # Stores the minimum number of
    # operations required
    ans = 0;
 
    # Update the value of ans
    for i in range(N):
        ans += abs(arr[i] - finalArray[i]);
 
    # Print the result
    print(ans // 2);
 
# Driver Code
arr = [1, 6];
N = len(arr);
countMinimumSteps(arr, N);
 
# This code is contributed by _saurabh_jaiswal.


C#
// C# program for the above approach
using System;
 
class GFG{
 
static void reverse(int[] a, int n)
{
    int i, t;
    for(i = 0; i < n / 2; i++)
    {
        t = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = t;
    }
}
 
// Function to minimize the operations
// for the difference between minimum
// and maximum element by incrementing
// decrementing array elements in pairs
static void countMinimumSteps(int[] arr, int N)
{
     
    // Stores the sum of the array
    int sum = 0;
 
    // Find the sum of array element
    for(int i = 0; i < N; i++)
    {
        sum += arr[i];
    }
 
    // Stores the resultant final array
    int[] finalArray = new int[N];
 
    // Iterate over the range [0, N]
    for(int i = 0; i < N; ++i)
    {
         
        // Assign values to finalArray
        if (i < sum % N)
        {
            finalArray[i] = sum / N + 1;
        }
        else
        {
            finalArray[i] = sum / N;
        }
    }
 
    // Reverse the final array
    reverse(finalArray, finalArray.Length);
 
    // Stores the minimum number of
    // operations required
    int ans = 0;
 
    // Update the value of ans
    for(int i = 0; i < N; ++i)
    {
        ans += Math.Abs(arr[i] - finalArray[i]);
    }
 
    // Print the result
    Console.WriteLine(ans / 2);
}
 
// Driver Code
public static void Main(String[] args)
{
    int[] arr = { 1, 6 };
    int N = arr.Length;
     
    countMinimumSteps(arr, N);
}
}
 
// This code is contributed by target_2


Javascript


输出:
2

时间复杂度: O(N*log N)
辅助空间: O(N)