Algorithm for linear time.