Remove exactly one element from the array such that max | function to calculate max - min ; There should be at - least two elements ; To store first and second minimums ; To store first and second maximums ; Driver code
< ? php function max_min ( $ a , $ n ) { if ( $ n <= 1 ) return PHP_INT_MAX ; $ f_min = $ a [ 0 ] ; $ s_min = PHP_INT_MAX ; $ f_max = $ a [ 0 ] ; $ s_max = ~ PHP_INT_MAX ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] <= $ f_min ) { $ s_min = $ f_min ; $ f_min = $ a [ $ i ] ; } else if ( $ a [ $ i ] < $ s_min ) { $ s_min = $ a [ $ i ] ; } if ( $ a [ $ i ] >= $ f_max ) { $ s_max = $ f_max ; $ f_max = $ a [ $ i ] ; } else if ( $ a [ $ i ] > $ s_max ) { $ s_max = $ a [ $ i ] ; } } return min ( ( $ f_max - $ s_min ) , ( $ s_max - $ f_min ) ) ; } $ a = array ( 1 , 3 , 3 , 7 ) ; $ n = sizeof ( $ a ) ; echo ( max_min ( $ a , $ n ) ) ; ? >