Bash Min Avg Two Slice
min_avg_two_slice() {
local -n _a="$1"
local _idx=0
local _min
_min=$(echo "scale=10; (${_a[0]} + ${_a[1]}) / 2" | bc)
local _count=${#_a[@]} _i
for ((_i = 0; _i < _count - 1; _i++)); do
local _cur
_cur=$(echo "scale=10; (${_a[_i]} + ${_a[_i+1]}) / 2" | bc)
if (( _i + 2 < _count )); then
local _three
_three=$(echo "scale=10; (${_a[_i]} + ${_a[_i+1]} + ${_a[_i+2]}) / 3" | bc)
if (( $(echo "$_three < $_cur" | bc -l) )); then
_cur=$_three
fi
fi
if (( $(echo "$_cur < $_min" | bc -l) )); then
_min=$_cur
_idx=$_i
fi
done
echo "$_idx"
}
This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.