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