Solved in linear time