better algorithm using an infinite array