Track the minimum price so far. At each day, profit = price - minSoFar. Keep max profit.
One pass. No nested loops.
Input
6 7 1 5 3 6 4
Output
5
5 7 6 4 3 1
0