Remove exactly one element from the array such that max | Python3 implementation of the above approach ; 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
import sys NEW_LINE def max_min ( a , n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return sys . maxsize NEW_LINE DEDENT f_min = a [ 0 ] NEW_LINE s_min = sys . maxsize NEW_LINE f_max = a [ 0 ] NEW_LINE s_max = - ( sys . maxsize - 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] <= f_min ) : NEW_LINE INDENT s_min = f_min NEW_LINE f_min = a [ i ] NEW_LINE DEDENT elif ( a [ i ] < s_min ) : NEW_LINE INDENT s_min = a [ i ] NEW_LINE DEDENT if ( a [ i ] >= f_max ) : NEW_LINE INDENT s_max = f_max NEW_LINE f_max = a [ i ] NEW_LINE DEDENT elif ( a [ i ] > s_max ) : NEW_LINE INDENT s_max = a [ i ] NEW_LINE DEDENT DEDENT return min ( ( f_max - s_min ) , ( s_max - f_min ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 3 , 3 , 7 ] NEW_LINE n = len ( a ) NEW_LINE print ( max_min ( a , n ) ) NEW_LINE DEDENT