Bash Max Profit
max_profit() {
local -n _a="$1"
local _price=${_a[0]} _profit=0 _v
for _v in "${_a[@]}"; do
(( _v < _price )) && _price=$_v
local _cur=$(( _v - _price ))
(( _cur > _profit )) && _profit=$_cur
done
echo "$_profit"
}
This tracks the lowest buy price seen so far and updates the best profit as it scans the prices once.