First implementation, use three pointers