linear time algorithm works