0(N) solution