log(N) solution by using two pointers