log(N) implementation