📌  相关文章
📜  通过删除单个数组元素来最小化相邻元素之间可能的最大差异

📅  最后修改于: 2021-09-06 06:26:26             🧑  作者: 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;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG
{
 
// Function to find maximum difference
// between adjacent array elements
static int maxAdjacentDifference(ArrayList A)
{
   
    // Store the maximum difference
    int diff = 0;
 
// Traverse the array
    for (int i = 1; i < (int)A.size(); i++)
    {
 
        // Update maximum difference
        diff = Math.max(diff, A.get(i) - A.get(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 = Integer.MAX_VALUE;
 
    for (int i = 0; i < N; i++) {
 
        // Stores the updated array
        ArrayList new_arr=new ArrayList<>();
 
        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;
   System.out.print(MinimumValue(arr, N));
 
    }
}
 
// This code is contributed by offbeat


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


Javascript


输出:
4

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live