📌  相关文章
📜  通过从数组中删除一个元素来最小化相邻差的总和

📅  最后修改于: 2021-04-29 17:01:58             🧑  作者: Mango

给定大小大于2的正整数数组,任务是找到数组的连续差分模量之和的最小值,即| A1-A0 | + | A2-A1 | + | A3-A2 | + ……+ | An-1-An-2 | + | An-An-1 |从数组中删除一个元素后,其中An表示数组元素值的第n个索引。

例子:

想法是从头到尾遍历数组,找到数组中的元素,移除后我们在该元素处获得最大连续模数差。从计算出的总值中减去最大值。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Function to find the element
int findMinRemoval(int arr[], int n)
{
    // Value variable for storing the total value
    int temp, value = 0;
  
    // Declaring maximum value as zero
    int maximum = 0;
  
    // If array contains on element
    if (n == 1)
        return 0;
  
    for (int i = 0; i < n; i++) {
  
        // Storing the maximum value in temp variable
        if (i != 0 && i != n - 1) {
            value = value + abs(arr[i] - arr[i + 1]);
  
            // Adding the adjacent difference modulus
            // values of removed element. Removing adjacent
            // difference modulus value after removing element
            temp = abs(arr[i] - arr[i + 1]) + 
                   abs(arr[i] - arr[i - 1]) -
                   abs(arr[i - 1] - arr[i + 1]);
        }
        else if (i == 0) {
            value = value + abs(arr[i] - arr[i + 1]);
            temp = abs(arr[i] - arr[i + 1]);
        }
        else
            temp = abs(arr[i] - arr[i - 1]);
  
        maximum = max(maximum, temp);
    }
  
    // Returning total value-maximum value
    return (value - maximum);
}
  
// Drivers code
int main()
{
    int arr[] = { 1, 5, 3, 2, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << findMinRemoval(arr, n) << "\n";
    return 0;
}


Java
// Java implementation of the approach
class GFG 
{
  
// Function to find the element
static int findMinRemoval(int arr[], int n)
{
    // Value variable for storing the total value
    int temp, value = 0;
  
    // Declaring maximum value as zero
    int maximum = 0;
  
    // If array contains on element
    if (n == 1)
        return 0;
  
    for (int i = 0; i < n; i++)
    {
  
        // Storing the maximum value in temp variable
        if (i != 0 && i != n - 1) 
        {
            value = value + Math.abs(arr[i] - arr[i + 1]);
  
            // Adding the adjacent difference modulus
            // values of removed element. Removing adjacent
            // difference modulus value after removing element
            temp = Math.abs(arr[i] - arr[i + 1]) + 
                Math.abs(arr[i] - arr[i - 1]) -
                Math.abs(arr[i - 1] - arr[i + 1]);
        }
        else if (i == 0) 
        {
            value = value + Math.abs(arr[i] - arr[i + 1]);
            temp = Math.abs(arr[i] - arr[i + 1]);
        }
        else
            temp = Math.abs(arr[i] - arr[i - 1]);
  
        maximum = Math.max(maximum, temp);
    }
  
    // Returning total value-maximum value
    return (value - maximum);
}
  
// Drivers code
public static void main(String[] args) 
{
    int arr[] = { 1, 5, 3, 2, 10 };
    int n = arr.length;
    System.out.print(findMinRemoval(arr, n) + "\n");
}
}
  
// This code contributed by Rajput-Ji


Python 3
# Python 3 implementation of above approach
  
# Function to find the element
def findMinRemoval(arr, n):
  
    # Value variable for storing the
    # total value
    value = 0
  
    # Declaring maximum value as zero
    maximum = 0
  
    # If array contains on element
    if (n == 1):
        return 0
  
    for i in range( n):
  
        # Storing the maximum value in 
        # temp variable
        if (i != 0 and i != n - 1):
            value = value + abs(arr[i] - arr[i + 1])
  
            # Adding the adjacent difference modulus
            # values of removed element. Removing 
            # adjacent difference modulus value after
            # removing element
            temp = (abs(arr[i] - arr[i + 1]) + 
                    abs(arr[i] - arr[i - 1]) -
                    abs(arr[i - 1] - arr[i + 1]))
          
        elif (i == 0):
            value = value + abs(arr[i] - arr[i + 1])
            temp = abs(arr[i] - arr[i + 1])
      
        else:
            temp = abs(arr[i] - arr[i - 1])
  
        maximum = max(maximum, temp)
  
    # Returning total value-maximum value
    return (value - maximum)
  
# Drivers code
if __name__ == "__main__":
  
    arr = [ 1, 5, 3, 2, 10 ]
    n = len(arr)
  
    print(findMinRemoval(arr, n))
  
# This code is contributed by ita_c


C#
// C# implementation of the approach 
using System;
  
class GFG 
{ 
  
    // Function to find the element 
    static int findMinRemoval(int []arr, int n) 
    { 
        // Value variable for storing the total value 
        int temp, value = 0; 
      
        // Declaring maximum value as zero 
        int maximum = 0; 
      
        // If array contains on element 
        if (n == 1) 
            return 0; 
      
        for (int i = 0; i < n; i++) 
        { 
      
            // Storing the maximum value in temp variable 
            if (i != 0 && i != n - 1) 
            { 
                value = value + Math.Abs(arr[i] - arr[i + 1]); 
      
                // Adding the adjacent difference modulus 
                // values of removed element. Removing adjacent 
                // difference modulus value after removing element 
                temp = Math.Abs(arr[i] - arr[i + 1]) + 
                    Math.Abs(arr[i] - arr[i - 1]) - 
                    Math.Abs(arr[i - 1] - arr[i + 1]); 
            } 
            else if (i == 0) 
            { 
                value = value + Math.Abs(arr[i] - arr[i + 1]); 
                temp = Math.Abs(arr[i] - arr[i + 1]); 
            } 
            else
                temp = Math.Abs(arr[i] - arr[i - 1]); 
      
            maximum = Math.Max(maximum, temp); 
        } 
      
        // Returning total value-maximum value 
        return (value - maximum); 
    } 
      
    // Driver code 
    public static void Main() 
    { 
        int []arr = { 1, 5, 3, 2, 10 }; 
        int n = arr.Length; 
        Console.WriteLine(findMinRemoval(arr, n)); 
    } 
} 
  
// This code contributed by Ryuga


PHP


输出:
7