code with dist array