peak end finding algorithm