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

📅  最后修改于: 2021-10-26 06:09:01             🧑  作者: Mango

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

  • 将任何后缀数组的所有数组元素加 1。
  • 将任何后缀数组的所有元素减 1。
  • 替换任何数组元素 y 另一个。

例子:

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

  • 初始化一个变量,比如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


Javascript


输出:
2

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程