public int MaxProfit(int[] prices) {
int smallestStockPrice = prices[0];
for(int i = 1; i < prices.Length; i++)
if(prices[i] < smallestStockPrice)
smallestStockPrice = prices[i];
profit = Math.Max(profit, prices[i] - smallestStockPrice);
public static void Main()
Program output = new Program();
int[] input = {7,1,5,3,6,4};
Console.WriteLine(output.MaxProfit(input));