human
What is a code to maximum difference between two elements such that larger element appears after the smaller number and the other steps required given the following:
Maximum difference between two elements such that larger element appears after the smaller number | The function assumes that there are at least two elements in array . The function returns a negative value if the array is sorted in decreasing order and returns 0 if elements are equal.
Initialize Result.
Initialize max element from right side.
Driver Code.
Function calling"
How can the above be solved in Python?
assistant
#Here is code to maximum difference between two elements such that larger element appears after the smaller number and the other steps required.
 def maxDiff ( arr , n ) :
	maxDiff = - 1
	maxRight = arr [ n - 1 ]
	for i in range ( n - 2 , - 1 , - 1 ) :
		if ( arr [ i ] > maxRight ) :
			maxRight = arr [ i ]
		else :
			diff = maxRight - arr [ i ]
			if ( diff > maxDiff ) :
				maxDiff = diff
	return maxDiff
if __name__ == ' _ _ main _ _ ' :
	arr = [ 1 , 2 , 90 , 10 , 110 ]
	n = len ( arr )
	print ( " Maximum ▁ difference ▁ is " , maxDiff ( arr , n ) )