I wanted to ask for a code review for the following interview question:
Problem: Max Gain
Given an list of integers, write a method - max_gain - that returns the maximum gain. Maximum Gain is defined as the maximum difference between 2 elements in a list such that the larger element appears after the smaller element. If no gain is possible, return 0.
Example:
max_gain([100,40,20,10]) ==> 0
max_gain([0,50,10,100,30]) ==> 100
Solution
def max_gain(input_list):
if len(input_list) < 2:
return 0
a = input_list[0]
max_val = 0
for i in input_list[1:]:
max_val = max(max_val, i-a)
a = min(a, i)
return max_val