This is my solution the problem posted here. It solves the problem for any lexographic permutation of any set of numbers: