using System.Collections.Generic;
public static void Main()
var allTests = new List<(List<int>, int)>
(new List<int> { 10, 7, 5, 8, 11, 9 }, 6),
(new List<int> { 10, 9, 11, 5, 8, 9 }, 4),
(new List<int> { 12, 11, 10, 9, 7, 6 }, -1),
(new List<int> { 10, 6, 11, 5, 8, 9 }, 5),
(new List<int> { 12, 10, 8, 6, 4, 2 }, -2),
(new List<int> { 2, 8, 1, 6, 3, 5 }, 6),
(new List<int> { 12, 6, 11, 5, 8, 9 }, 5),
foreach (var (stockPrices, expected) in allTests)
Console.WriteLine($"Found Max profit: {GetMaxProfit(stockPrices)} Expected: {expected}");
public static int GetMaxProfit(IReadOnlyList<int> stockPrices)
if (stockPrices.Count < 2)
var min = stockPrices[0];
for (int i = 1; i < stockPrices.Count; ++i)
var temp = stockPrices[i] - min;
if (min > stockPrices[i])
if (profit is null || temp > profit)