Minimize maximum difference between adjacent elements possible by removing a single array element | Java program for 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
import java . util . * ; class GFG { static int maxAdjacentDifference ( ArrayList < Integer > A ) { int diff = 0 ; for ( int i = 1 ; i < ( int ) A . size ( ) ; i ++ ) { diff = Math . max ( diff , A . get ( i ) - A . get ( i - 1 ) ) ; } return diff ; } static int MinimumValue ( int arr [ ] , int N ) { int MinValue = Integer . MAX_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { ArrayList < Integer > new_arr = new ArrayList < > ( ) ; for ( int j = 0 ; j < N ; j ++ ) { if ( i == j ) continue ; new_arr . add ( arr [ j ] ) ; } MinValue = Math . min ( MinValue , maxAdjacentDifference ( new_arr ) ) ; } return MinValue ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 7 , 8 } ; int N = arr . length ; System . out . print ( MinimumValue ( arr , N ) ) ; } }