---
# System prepended metadata

title: Untitled

---

Given an array of stock prices for a day, where prices[i] is the price of the stock at time i, write a function that returns the maximum amount of money one could make in a day if he buys and sells at most once.

Input: [4,6,1,2,5,2,8] -> 7

Input: [6,3,1] -> 0

Input: [4,5,6,1,2] -> 2

```javascript
// 
function func(prices) {
    let minVal = prices[0]; // 4
    let maxDelta = -100000000; // 0
    prices.foreach((p) => { // 1
        let delta = p - minVal; // 1 - 4 = -3
        if (delta > maxDelta) { // 2 > 0
            maxDelta = delta; // maxDelta = 2
        }
        if (p < minVal) { // 1 < 4
            minVal = p; // minVal = 1
        }
    });
    return maxDelta;
}