longest continuous segment algorithm