human
Minimize the maximum minimum difference after one removal from array | Function to return the minimum required difference.
If current element is greater than max.
max will become secondMax.
Update the max.
If current element is greater than secondMax but smaller than max.
Update the secondMax.
If current element is smaller than min.
min will become secondMin.
Update the min.
If current element is smaller than secondMin but greater than min.
Update the secondMin.
Minimum of the two possible differences.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to minimize the maximum minimum difference after one removal from array and the other steps required.
 def findMinDifference ( arr , n ) :
	if ( arr [ 0 ] < arr [ 1 ] ) :
		min__ = secondMax = arr [ 0 ]
	else :
		min__ = secondMax = arr [ 1 ]
	if ( arr [ 0 ] < arr [ 1 ] ) :
		max__ = secondMin = arr [ 1 ]
	else :
		max__ = secondMin = arr [ 0 ]
	for i in range ( 2 , n ) :
		if ( arr [ i ] > max__ ) :
			secondMax = max__
			max__ = arr [ i ]
		elif ( arr [ i ] > secondMax ) :
			secondMax = arr [ i ]
		elif ( arr [ i ] < min__ ) :
			secondMin = min__
			min__ = arr [ i ]
		elif ( arr [ i ] < secondMin ) :
			secondMin = arr [ i ]
	diff = min ( max__ - secondMin , secondMax - min__ )
	return diff
if __name__ == ' _ _ main _ _ ' :
	arr = [ 1 , 2 , 4 , 3 , 4 ]
	n = len ( arr )
	print ( findMinDifference ( arr , n ) )