using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
public static int GetMaxProfit(int[] stockPrices)
if(stockPrices.Length < 2)
throw new System.Exception("max profits requires 2 or more stock prices to compare!");
int maxProfit = stockPrices[1] - stockPrices[0];
int lowestPrice = stockPrices[0];
for(int i = 1; i < stockPrices.Length; i++)
int potentialProfit = stockPrices[i] - lowestPrice;
maxProfit = Math.Max(potentialProfit, maxProfit);
lowestPrice = Math.Min(lowestPrice, stockPrices[i]);
public static void Main()
int[] stocks = new int[] { 500, 506, 503, 507, 502, 510, 509, 512, 510, 509, 510 };
int maxProfit = GetMaxProfit(stocks);
Console.WriteLine(maxProfit);