def max_profit(prices): # Initialize two pointers and maximum profit variable buy = 0 sell = 1 maxprofit = 0 # Iterating over the prices array while sell < len(prices): # Calculating the current profit currentprofit = prices[sell] - prices[buy] # Setting the maximum profit if price at sell pointer is greater than buy if prices[buy] < prices[sell]: maxprofit = max(currentprofit, maxprofit) # Otherwise, update buy to be equal to sell else: buy = sell # Moving sell pointer to the next index of the prices array sell += 1 return maxprofit # Driver code def main(): prices = [ [1, 2, 4, 2, 5, 7, 2, 4, 9, 0, 9], [8, 2, 6, 4, 7, 5], [7, 6, 4, 3, 1], [2, 6, 8, 7, 8, 7, 9, 4, 1, 2, 4, 5, 8], [1, 2] ] for i in range(len(prices)): print(i + 1, ". Stock prices = ", prices[i], sep="") print(" Maximum profit = ", max_profit(prices[i]), sep="") print("-" * 100) if __name__ == "__main__": main()