📌  相关文章
📜  通过删除单个数组元素,最大可能地减少相邻元素之间的最大差异

📅  最后修改于: 2021-05-17 20:45:32             🧑  作者: Mango

给定一个由N个元素组成的排序数组arr [] ,任务是找到通过删除任何单个数组元素而获得的所有数组的相邻元素之间的所有最大差异中的最小值。

例子:

方法:按照步骤解决问题

  • 声明一个变量MinValue = INT_MAX存储最终答案。
  • 遍历数组, i范围为[0,N – 1]
    • 声明一个向量new_arr ,它是arr []的副本,但元素arr [i]除外
    • 将new_arr的最大相邻差值存储在变量diff中
    • 更新MinValue = min(MinValue,diff)
  • 返回MinValue作为最终答案。

下面是上述方法的实现。

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find maximum difference
// between adjacent array elements
int maxAdjacentDifference(vector A)
{
    // Store the maximum difference
    int diff = 0;
 
// Traverse the array
    for (int i = 1; i < (int)A.size(); i++) {
 
        // Update maximum difference
        diff = max(diff, A[i] - A[i - 1]);
    }
 
    return diff;
}
 
// Function to calculate the minimum
// of maximum difference between
// adjacent array elements possible
// by removing a single array element
int MinimumValue(int arr[], int N)
{
    // Stores the required minimum
    int MinValue = INT_MAX;
 
    for (int i = 0; i < N; i++) {
 
        // Stores the updated array
        vector new_arr;
 
        for (int j = 0; j < N; j++) {
 
            // Skip the i-th element
            if (i == j)
                continue;
 
            new_arr.push_back(arr[j]);
        }
 
        // Update MinValue
        MinValue
            = min(MinValue,
                  maxAdjacentDifference(new_arr));
    }
 
    // return MinValue
    return MinValue;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 3, 7, 8 };
    int N = sizeof(arr) / sizeof(int);
    cout << MinimumValue(arr, N);
 
    return 0;
}


Python3
# Python3 program to implement
# the above approach
import sys
 
# Function to find maximum difference
# between adjacent array elements
def maxAdjacentDifference(A):
     
    # Store the maximum difference
    diff = 0
 
    # Traverse the array
    for i in range(1, len(A), 1):
         
        # Update maximum difference
        diff = max(diff, A[i] - A[i - 1])
 
    return diff
 
# Function to calculate the minimum
# of maximum difference between
# adjacent array elements possible
# by removing a single array element
def MinimumValue(arr, N):
     
    # Stores the required minimum
    MinValue = sys.maxsize
 
    for i in range(N):
         
        # Stores the updated array
        new_arr = []
 
        for j in range(N):
             
            # Skip the i-th element
            if (i == j):
                continue
 
            new_arr.append(arr[j])
 
        # Update MinValue
        MinValue = min(MinValue,
                       maxAdjacentDifference(new_arr))
 
    # return MinValue
    return MinValue
 
# Driver Code
if __name__ == '__main__':
     
    arr =  [ 1, 3, 7, 8 ]
    N = len(arr)
     
    print(MinimumValue(arr, N))
 
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find maximum difference
// between adjacent array elements
static int maxAdjacentDifference(List A)
{
     
    // Store the maximum difference
    int diff = 0;
 
    // Traverse the array
    for(int i = 1; i < A.Count; i++)
    {
         
        // Update maximum difference
        diff = Math.Max(diff, A[i] - A[i - 1]);
    }
    return diff;
}
 
// Function to calculate the minimum
// of maximum difference between
// adjacent array elements possible
// by removing a single array element
static int MinimumValue(int[] arr, int N)
{
     
    // Stores the required minimum
    int MinValue = Int32.MaxValue;
 
    for(int i = 0; i < N; i++)
    {
         
        // Stores the updated array
        List new_arr = new List();
 
        for(int j = 0; j < N; j++)
        {
             
            // Skip the i-th element
            if (i == j)
                continue;
 
            new_arr.Add(arr[j]);
        }
 
        // Update MinValue
        MinValue = Math.Min(MinValue,
             maxAdjacentDifference(new_arr));
    }
 
    // Return MinValue
    return MinValue;
}
 
// Driver Code
public static void Main(string[] args)
{
    int[] arr = { 1, 3, 7, 8 };
    int N = arr.Length;
     
    Console.WriteLine(MinimumValue(arr, N));
}
}
 
// This code is contributed by ukasp


输出:
4

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