Refined code by boolean array to track used numbers