human
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 diff , current sum and max sum.
Calculate current diff.
Calculate current sum.
Update max sum , if needed.
Driver Code.
Function calling"
Solution in Python:
 What is a code to maximum difference between two elements such that larger element appears after the smaller number and the other steps required?
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 ) :
	diff = arr [ 1 ] - arr [ 0 ]
	curr_sum = diff
	max_sum = curr_sum
	for i in range ( 1 , n - 1 ) :
		diff = arr [ i + 1 ] - arr [ i ]
		if ( curr_sum > 0 ) :
			curr_sum += diff
		else :
			curr_sum = diff
		if ( curr_sum > max_sum ) :
			max_sum = curr_sum
	return max_sum
if __name__ == ' _ _ main _ _ ' :
	arr = [ 80 , 2 , 6 , 3 , 100 ]
	n = len ( arr )
	print ( " Maximum ▁ difference ▁ is " , maxDiff ( arr , n ) )