📌  相关文章
📜  通过执行给定的操作最少次数使数组元素相等

📅  最后修改于: 2021-04-18 02:25:35             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是通过执行以下操作最少次数来使所有数组元素相等:

  • 将任何后缀数组的所有数组元素增加1。
  • 将任何后缀数组的所有元素减少1。
  • 替换任何数组元素。

例子:

方法:这个想法是找到实际总和与所有元素相等的数组之和之间的差,然后选择要执行的运算,从而导致最少的运算数。请按照以下步骤解决问题:

  • 初始化一个变量,例如totOps ,以存储使所有数组元素相等所需的实际操作。
  • 遍历数组并存储所有成对的连续元素之间的差,并将它们的和存储在totOps中
  • 初始化一个变量,例如maxOps ,以存储所需的最大操作数。
  • 现在,找到在更改元素时发生的最大更改并将其存储在maxOps变量中。有以下三种情况:
    • 对于第一个元素,即arr [1] ,更改arr [1]的最佳方法是使其变为arr [2]
    • 对于最后一个元素,即arr [N] ,更改arr [N]的最佳方法是使其变为arr [N-1]
    • 对于元素的其余部分,更改arr [i]同时影响arr [i-1]arr [i + 1] ,因此,最大更改为abs(arr [i] – arr [i + 1])+ abs(arr [i] – arr [i-1])– abs(arr [i-1] – arr [i + 1)。
  • 因此,所需的最小操作数等于totOpsmaxOps之间的差。

下面是上述方法的实现:

C++
// C++ Program for the above approach
 
#include 
using namespace std;
 
// Function to calculate the minimum
// operations of given type required
// to make the array elements equal
void minOperation(int a[], int N)
{
    // Stores the total count of operations
    int totOps = 0;
 
    // Traverse the array
    for (int i = 0; i < N - 1; i++) {
 
        // Update difference between
        // pairs of adjacent elements
        totOps += abs(a[i] - a[i + 1]);
    }
 
    // Store the maximum count of operations
    int maxOps
        = max(abs(a[0] - a[1]),
              abs(a[N - 1] - a[N - 2]));
 
    for (int i = 1; i < N - 1; i++) {
 
        // Rest of the elements
        maxOps
            = max(maxOps, abs(a[i] - a[i - 1])
                              + abs(a[i] - a[i + 1])
                              - abs(a[i - 1] - a[i + 1]));
    }
 
    // Total Operation - Maximum Operation
    cout << totOps - maxOps << endl;
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 1, -1, 0, 1, 1 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    minOperation(arr, N);
 
    return 0;
}


Java
// Java Program for the above approach
import java.io.*;
class GFG
{
 
  // Function to calculate the minimum
  // operations of given type required
  // to make the array elements equal
  static void minOperation(int a[], int N)
  {
 
    // Stores the total count of operations
    int totOps = 0;
 
    // Traverse the array
    for (int i = 0; i < N - 1; i++)
    {
 
      // Update difference between
      // pairs of adjacent elements
      totOps += Math.abs(a[i] - a[i + 1]);
    }
 
    // Store the maximum count of operations
    int maxOps
      = Math.max(Math.abs(a[0] - a[1]),
                 Math.abs(a[N - 1] - a[N - 2]));
 
    for (int i = 1; i < N - 1; i++)
    {
 
      // Rest of the elements
      maxOps = Math.max(
        maxOps,
        Math.abs(a[i] - a[i - 1])
        + Math.abs(a[i] - a[i + 1])
        - Math.abs(a[i - 1] - a[i + 1]));
    }
 
    // Total Operation - Maximum Operation
    System.out.println(totOps - maxOps);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    // Given array
    int[] arr = { 1, -1, 0, 1, 1 };
 
    // Size of the array
    int N = arr.length;
 
    minOperation(arr, N);
  }
}
 
// This code is contributed by Dharanendra L V


Python3
# Python3 Program for the above approach
 
# Function to calculate the minimum
# operations of given type required
# to make the array elements equal
def minOperation(a, N):
   
    # Stores the total count of operations
    totOps = 0
 
    # Traverse the array
    for i in range(N - 1):
 
        # Update difference between
        # pairs of adjacent elements
        totOps += abs(a[i] - a[i + 1])
 
    # Store the maximum count of operations
    maxOps = max(abs(a[0] - a[1]), abs(a[N - 1] - a[N - 2]))
    for i in range(1, N - 1):
 
        # Rest of the elements
        maxOps = max(maxOps, abs(a[i] - a[i - 1]) +
                     abs(a[i] - a[i + 1])- abs(a[i - 1] - a[i + 1]))
 
    # Total Operation - Maximum Operation
    print (totOps - maxOps)
 
# Driver Code
if __name__ == '__main__':
   
    # Given array
    arr = [1, -1, 0, 1, 1]
 
    # Size of the array
    N = len(arr)
    minOperation(arr, N)
 
# This code is contributed by mohit kumar 29.


C#
// C# Program for the above approach
using System;
public class GFG {
 
  // Function to calculate the minimum
  // operations of given type required
  // to make the array elements equal
  static void minOperation(int[] a, int N)
  {
    // Stores the total count of operations
    int totOps = 0;
 
    // Traverse the array
    for (int i = 0; i < N - 1; i++)
    {
 
      // Update difference between
      // pairs of adjacent elements
      totOps += Math.Abs(a[i] - a[i + 1]);
    }
 
    // Store the maximum count of operations
    int maxOps
      = Math.Max(Math.Abs(a[0] - a[1]),
                 Math.Abs(a[N - 1] - a[N - 2]));
 
    for (int i = 1; i < N - 1; i++)
    {
 
      // Rest of the elements
      maxOps = Math.Max(
        maxOps,
        Math.Abs(a[i] - a[i - 1])
        + Math.Abs(a[i] - a[i + 1])
        - Math.Abs(a[i - 1] - a[i + 1]));
    }
 
    // Total Operation - Maximum Operation
    Console.WriteLine(totOps - maxOps);
  }
 
  // Driver Code
  static public void Main()
  {
 
    // Given array
    int[] arr = { 1, -1, 0, 1, 1 };
 
    // Size of the array
    int N = arr.Length;
 
    minOperation(arr, N);
  }
}
 
// This code is contributed by Dharanendra L V


输出:
2

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