two pointer solution