Google interview question

solving with two pointer question