Algorithm to find next highest number